在 Objective-C 中将变量传递给导航控制器中嵌入的视图控制器

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

我已经在导航控制器中嵌入了目的地 viewcontroller,但现在无法向其传递变量。

我的代码是:

    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];

在VC发射后,发件人属性为nil。它没有得到值@1.

我错过了什么?

感谢您的任何建议。

在您的代码中,您有一条评论说:

//This is the view controller embedded in the nav

但是,该评论下方的视图控制器不是嵌入在您的导航控制器中的视图控制器。这是一个全新的控制器,在该行创建并在函数结束时处理。

您还需要这样的东西:

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

以上可能存在一些语法问题...