5

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?

Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • What do you mean "control"? Why do you need to? – Phillip Mills May 26 '17 at 12:17
  • for example. i would like to add an new row into the tableview of VC_A from Class VC_B – Trombone0904 May 26 '17 at 12:52
  • fore more details: I tried this tutorial: http://www.knowstack.com/swift-nstableview-drag-drop-in/ both tableviews are in one view controller and using one class. i would like to realize, that both table views are in different view controllers (split view) und the code in two different classes – Trombone0904 May 28 '17 at 08:59

1 Answers1

4

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()
adamfowlerphoto
  • 2,708
  • 1
  • 11
  • 24
  • and where is the assign of each table view as a IBoutlet ? – Trombone0904 May 29 '17 at 07:25
  • There will be one in each VC I guess. Why do you need the IBOutlet? Is it just to connect with a table view setup in Storyboard – adamfowlerphoto May 29 '17 at 07:31
  • Well then you have a single IBOutlet in each VC for the table view it contains. The protocol should contain functions for manipulating this table view which you can then call from the other VC. – adamfowlerphoto May 29 '17 at 07:38