10

在故事板中,我有一个介绍性的视图控制器,它淡入 UINavigationController。

      |   |      |   |
   -> |   |  ->  |   |     
      |   |      |   |
     ViewCtrl   NavCtrl

我想从应用程序委托中获取对导航控制器的引用(就像@Guillaume 回答在这个问题中所做的那样)。

这是我尝试访问它的方式:

UIApplication.sharedApplication.delegate.window.rootViewController.navigationController;

但是导航控制器是零。

任何概念为什么 navController 是 nil 或者我可以做些什么来获得对它的引用?

4

2 回答 2

25
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

UINavigationController *controller = (UINavigationController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];

您在导航控制器的属性检查器中为导航控制器指定控制器标识符。

于 2013-08-23T03:15:33.410 回答
2

所以 Arsen 的解决方案看起来不错,但我不知道如何获得故事板的名称,哈哈。对我有用的是

UINavigationController *navigationController = (UINavigationController*)_window.rootViewController;
AVDashboardViewController *dashBoardViewController =
(AVDashboardViewController*)[navigationController.viewControllers  objectAtIndex:1];//since on index 0 I have my login screen and index 1 is the home screen

在 AppDelegate 上获取 NavigationController 的引用似乎是不好的编码习惯,但在我的情况下,我需要它,因为它是

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

定位,这是收到Notification时调用的方法之一。收到消息后,我还在为如何更新导航控制器而苦苦挣扎,所以可能跑题了,但这里有一个免费提示:

UIBarButtonItem *notiButton = [[UIBarButtonItem alloc] initWithTitle: @"Received push!" style: UIBarButtonItemStyleBordered target:self action:nil];

[[dashBoardViewController navigationItem] setLeftBarButtonItem:notiButton animated:YES];

[navigationController popToViewController:dashBoardViewController animated:YES];

希望能帮助到你!

于 2014-02-25T18:39:09.557 回答