Container Addition

I have a ViewController with a Container View that has a built-in TableViewController .

Now I would like to access the TableView in the ViewController , how can I make an exit for this?

I tried to add a Container View as an output, but I cannot access the built-in TableViewController .

enter image description here

+6
source share
2 answers

You cannot directly exit because the table view is in a different scene (view controller), but you can access the table view as soon as you have a reference to the UITableViewController instance. There are several ways to do this.

First you can use the childViewControllers property of your subclass of UIViewController . If you know that there is only one child, then you can access it directly, otherwise you need to determine which one is correct, say, by cycling through the array.

 let myTableViewController = self.childViewControllers[0] as! UITableViewController let theTableView = myTableViewController.tableView 

The second option is to access the UITableViewController during the implementation of segue. If you click on the built-in segue in your storyboard, you can give it an identifier, like in any other segment. Then you can implement prepareForSegue and grab a built-in instance of the UITableViewController -

 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "tableviewEmbed") { let myTableViewController = segue.destinationViewController as! UITableViewController let theTableView = myTableViewController.tableView } } 

Personally, I prefer this second approach, because I think it is β€œcleaner”

+12
source

Create an output in the View child controller and access it using self.childViewControllers.lastObject (if you have only one child, otherwise use objectAtIndex :)

+1
source

All Articles