简体   繁体   中英

Pass variable to View Controller Embedded in Navigation Controller in Objective-C

I have embedded a destination viewcontroller in a navigation controller and am now unable to pass it a variable.

My code is:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//THis is the navigation controller 
    UIViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"myNav"];
//This is the view controller embedded in the nav
    IDNowVC* myVC = [storyboard instantiateViewControllerWithIdentifier:@"myVC"];
   myVC.sender = @1;//for contacts

   [self presentViewController:destVC animated:YES completion:nil];

After the launch of the VC, sender property is nil. It is not getting the value @1.

What am I missing?

Thanks for any suggestions.

In your code you have a comment that says:

//This is the view controller embedded in the nav

However, the view controller below that comment is not the one embedded in your navigation controller. It's a completely new controller that is created at that line and disposed of at the end of the function.

You need something more like this:

UINavigationController *destVC = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:@"myNav"];
IDNowVC* myVC = destVC.childViewControllers[0];
myVC.sender = @1; 

There might be some syntax issues with the above...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM