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
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.
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 .
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 .
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];
}
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With