Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContainerView add outlet

I have a ViewController with a Container View that has an embedded TableViewController.

Now i would like to access the TableView in the ViewController, how can i make an outlet for that?

I tried to add the Container View as an outlet, but i can't access the embedded TableViewController.

enter image description here

like image 663
Lord Vermillion Avatar asked Sep 08 '15 09:09

Lord Vermillion


2 Answers

You can't make an outlet directly because the table view is in a different scene (view controller), but you can access the tableview once you have a reference to the UITableViewController instance. There are a couple of different ways of doing that.

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

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

The second option is to access the UITableViewController during the embed segue. If you click on the embed segue in your storyboard you can give it an identifier as with any other segue. Then you can implement prepareForSegue and grab the embedded UITableViewController instance -

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 as I think it is 'cleaner'

like image 62
Paulw11 Avatar answered Oct 22 '22 23:10

Paulw11


Create outlet in child View Controller and access it using self.childViewControllers.lastObject (assuming you only have one child, otherwise use objectAtIndex:)

like image 1
Meghs Dhameliya Avatar answered Oct 22 '22 23:10

Meghs Dhameliya