Different classes work together

I am working with fast 3 for macOS and I have a general question. My storyboard has two view controllers with a table view for each view controller.

Example: View Controller A> VC_A.class View Controller B> VC_B.class

Both view controllers are elements of the same Split View controller. Now I would like to put one line item from VC A to VC B by drag and drop. this works great if both VCs are in the same class.

but now I would like to split it, as in the example below (VC_A and VC_B.class)

but how can I control tblview iboutlet VC_A in VC_B.class?

+5
source share
1 answer

You can use delegates and protocols for this. Set the protocol class using the interface for editing tables, for example

protocol EditableTableView { func insertCell() } 

For both of your ViewControllers, set them to comply with this protocol, execute the insertCell function, and add a delegate pointer.

 class ViewControllerA : EditableTableView { func insertCell() { ... add your code to insert a cell into VC A... } weak var otherTableViewDelegate : EditableTableView? } class ViewControllerB : EditableTableView { func insertCell() { ... add your code to insert a cell into VC B... } weak var otherTableViewDelegate : EditableTableView? } 

In your parent split VC, you can configure delegate pointers to point to a different view controller

 viewControllerA.otherTableViewDelegate = viewControllerB viewControllerB.otherTableViewDelegate = viewControllerA 

Now, when you want to insert a cell into another controller, you can call

 self.otherTableViewDelegate?.insertCell() 
+4
source

Source: https://habr.com/ru/post/1268251/


All Articles