簡體   English   中英

從AppDelegate獲取storyboard定義的UINavigationController

[英]Get storyboard-defined UINavigationController from AppDelegate

在storyboard中,我有一個介紹視圖控制器,它會淡化為UINavigationController。

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

我想從應用程序委托中獲得對導航控制器的引用(就像@Guillaume在這個問題中的回答一樣)。

這是我試圖訪問它的方式:

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

但導航控制器是零。

任何想法為什么navController是零或我可以做什么來獲得它的引用?

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

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

您可以在導航控制器的屬性檢查器中指定導航控制器的控制器標識符。

所以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 

找到,這是收到通知時調用的方法之一。 一旦收到消息,我也在努力解決如何更新導航控制器的問題,所以可能是關於主題的,但這里有一個免費提示:

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

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

[navigationController popToViewController:dashBoardViewController animated:YES];

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM