简体   繁体   English

从AppDelegate推送到特定的viewController

[英]Push to specific viewController from AppDelegate

My application hierarchy is looking like this: 我的应用程序层次结构如下所示:

tabBarController >> navigationControllers (multiple) >> viewControllers (multiple)

When a certain event occur in the appDelegate I want the push a specific viewController in one of the navigationControllers. 当某个事件发生在appDelegate我想推在navigationControllers的一个特定的viewController。 For now I have this: 现在我有这个:

UINavigationController *myNavigationController = [[(UITabBarController*)self.window.rootViewController viewControllers] objectAtIndex:0];

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
StoreViewController *targetViewController = (StoreViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"StoreViewController"];

[myNavigationController pushViewController:targetViewController animated:YES];

This only works if the application is currently in the same navigationController as the targetViewController. 仅当应用程序当前与targetViewController位于相同的NavigationController中时,此方法才有效。 I want it to work regardless of where you are in the application. 无论您在应用程序中的哪个位置,我都希望它能正常工作。 Thanks! 谢谢!

You need to programmatically switch to tab 0 where that particular navigation controller is, before you do the push. 在执行推送之前,您需要以编程方式切换到特定导航控制器所在的选项卡0。

UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
tbc.selectedIndex = 0;
UINavigationController *myNavigationController = tbc.selctedViewController;
etc......

The above code assumes that you want to push targetViewController from a particular navigation controller (the one in tab 0), but if you don't care which navigation controller pushes it, then just delete the tbc.selectedIndex = 0 line. 上面的代码假定您要从特定的导航控制器(选项卡0中的那个)推送targetViewController,但是如果您不关心哪个导航控制器将其推送,则只需删除tbc.selectedIndex = 0行即可。 The following line will push from which ever navigation controller is currently the selected one. 下一行将推入当前从哪个导航控制器中选择的那个。 I'm not sure from your question, which you want. 从您的问题中我不确定您要哪个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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