ios:从应用程序委托访问导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14126932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios: Accessing a navigation controller from app delegate
提问by James Harpe
I have an app that receives push notifications. In didReceiveRemoteNotifications, I would like to make the app show a particular view controller in the app's navigation controller (which happens to be the root view controller). What is the best way to make this happen? Can I get a reference to the navigation controller in the app delegate?
我有一个接收推送通知的应用程序。在 didReceiveRemoteNotifications 中,我想让应用程序在应用程序的导航控制器(恰好是根视图控制器)中显示一个特定的视图控制器。实现这一目标的最佳方法是什么?我可以在应用程序委托中获得对导航控制器的引用吗?
EDIT:Here is the code I'm trying to use right now. It appears to use the correct navigation controller, but it doesn't display the view controller at all, just a blank screen:
编辑:这是我现在正在尝试使用的代码。它似乎使用了正确的导航控制器,但它根本不显示视图控制器,只是一个空白屏幕:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
EventDetailViewController *destCon = [storyboard instantiateViewControllerWithIdentifier:@"EventDetailViewController"];
destCon.event=notifyEvent;
UINavigationController *navController =(UINavigationController *) self.window.rootViewController;
[navController pushViewController:destCon animated:YES];
Here is what I'm seeing:
这是我所看到的:
回答by rdelmar
If your navigation controller is the root view controller of the window, then you can just use
如果您的导航控制器是窗口的根视图控制器,那么您可以使用
(UINavigationController *)self.window.rootViewController
from the app delegate to access the one you created in the storyboard.
从应用程序委托访问您在故事板中创建的那个。
回答by u.gen
I use something like below it works for me, try to modify for your usage
我使用类似下面的东西对我有用,尝试修改以供您使用
Stoaryboard
is the name of your storyboard, when you recieve notification you can call your rootview LoginViewController
Stoaryboard
是您的故事板的名称,当您收到通知时,您可以调用您的 rootview LoginViewController
Make sure your viewcontroller in interface builder is set to yourControllerName
in below example it is LoginView
确保您的界面构建器中的视图控制器yourControllerName
在下面的示例中设置 为LoginView
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//replace and push rootview manually
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
LoginViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
}