Trichophyton
Trichophyton

Reputation: 645

Xcode - Pass property to view controller in storyboard

I would like to pass properties to a view controller created from a container view in a storyboard. The problem is that I don't know how I could do it.

The blue rectangle is the area where I have two containers view, each one referring to a view controller (one is the custom table view controller at the top and I am interested with and the other one to the view controller below).

The problem is that my custom table view controller needs some properties. I would like to pass the properties from the class where I instantiate the Storyboard (the storyboard is called from an toher class). The view controller containing the container views is instantiated like shown below:

enter image description here

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Phenotype" bundle:nil];
        GeneralViewController *vc = [sb instantiateViewControllerWithIdentifier:@"phenotype"];
        [vc setProperty:property]; // I would like to pass this property to the custom table view controller.
        [self.navigationController pushViewController:vc animated:YES];

I try to get the table view controller from the class where I instantiate the stroyboard to set the property at this time

TableViewController *tablevc = [sb instantiateViewControllerWithIdentifier:@"table"];
    [tablevc setProperty:property];

...but without any success.

Would anyone have an idea how I can access the property from the custom table view controller directly from the view controller which is instantiating the storyboard?

Thanks for your help,

Upvotes: 0

Views: 754

Answers (1)

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

Select your embed segue from blue rectangle to the tableView on top and give it a segue identifier :) lets say segue identifier is "abcd".

In your ViewController having container write

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "abcd" {
            let tableView : YourTableViewClass = segue.destination as! YourTableViewClass
            //pass whatever params want :)
        }
    }

EDIT

Embedded segues behaves just like other segues with one exception that other segues you will have to execute them either programmatically or triggering some event where as embedded segue gets executed automatically when a container loads :)

So you can have segue identifier, write code in prepareForSegue just like you do it for other segues :)

Upvotes: 3

Related Questions