Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to get a reference to a view controller embedded in a storyboard container with a segue?

I have a storyboard and a view controller with a container view. Within a storyboard I've defined a connection from the container to another view controller using "Embed" segue. How can I get a reference to the embedded view controller from within a parent view controller?

I've created a reference to the container, but see that it is just a UIView

Here's the segue I'm using enter image description here

enter image description here

like image 763
Alex Stone Avatar asked Aug 21 '14 21:08

Alex Stone


People also ask

How do I segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How do you give a segue an identifier in a storyboard?

Assigning an identifier for the segue:Select the segue, from the attribute inspector you'll see "Identifier" text field, that's it! make sure to insert the exact same name that used in performSegueWithIdentifier .

How do I embed a view controller in navigation controller storyboard?

Storyboard setup In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


2 Answers

you must implement the prepareForSegue in main ViewController and specify an identifier in your StoryBoard.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

    }
}
like image 90
tdelepine Avatar answered Sep 25 '22 11:09

tdelepine


Same answer as above, but in swift:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "nameOfSegueIdentiferFromStoryboard") {
        guard let destinationVC = segue.destination as? ViewControllerClassName else { return }
        destinationVC.someProperty = someValue
    }
}
like image 29
MischkaTheBear Avatar answered Sep 22 '22 11:09

MischkaTheBear