Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different classes work together

Tags:

I work with swift 3 for macOS and I have a general question. In my Storyboard are two View Controllers with a tableview for each View Controller.

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

Both View Controllers are elements of one Split View Controller. now i would like to put one row element form VC A to VC B via drag and drop. this works fine, if both VC are in one class.

but now i would like to split it like the example below (VC_A and VC_B.class)

but how can i control the iboutlet tblview of VC_A in the VC_B.class?

like image 917
Ghost108 Avatar asked May 26 '17 09:05

Ghost108


1 Answers

You could use delegates and protocols for this. Setup a protocol class with your interface for editing tables eg

protocol EditableTableView {
    func insertCell()
}

For both of your ViewControllers, set them to adhere to this protocol, implement the insertCell function and also 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 setup the delegate pointers so they point to the other view controller

viewControllerA.otherTableViewDelegate = viewControllerB
viewControllerB.otherTableViewDelegate = viewControllerA

Now whenever you want to insert a cell in the other controller you can call

self.otherTableViewDelegate?.insertCell()
like image 154
adamfowlerphoto Avatar answered Nov 15 '22 05:11

adamfowlerphoto