收到推送消息时从Appdelegate导航

时间:2014-07-21 12:18:00

标签: ios objective-c push-notification apple-push-notifications

目前我的方法就是这样的

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
 NSString *screenNo =[userInfo objectForKey:@"screen"];

}

基于screenNo我想导航到不同的视图控制器。但我不能像下面给出的大部分答案那样做。

原因是我的根视图不是导航控件,所以我无法思考。它崩溃了应用程序。

当推送消息到达时didReceiveRemoteNotification被调用,我也可以看到消息的内容。但它不会使用此处显示的方法进行导航。

    [self.window makeKeyAndVisible];
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"galleryViewController2"];
    [(UINavigationController *)self.window.rootViewController pushViewController:vc animated:YES];

这是例外

2014-07-21 18:06:53.709 Proitzen Rest[993:60b] -[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270
2014-07-21 18:06:53.712 Proitzen Rest[993:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270'
*** First throw call stack:
(0x2f480fd3 0x3a021ccf 0x2f484967 0x2f483253 0x2f3d27b8 0xff93b 0x31eb3b29 0x31eb37fb 0x31dbb05f 0x31e6d377 0x31d1c6f5 0x31c9555b 0x2f44c2a5 0x2f449c49 0x2f449f8b 0x2f3b4f0f 0x2f3b4cf3 0x342da663 0x31d0016d 0x157e69 0x3a52eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

感谢您的提前时间。

5 个答案:

答案 0 :(得分:1)

您正在尝试使用UIViewController推送UIViewController。这是不可能的。您的应用层次结构中必须有UINavigationController。 您也可以设置rootViewController:

[self.window setRootViewController: newViewController];

答案 1 :(得分:1)

你尝试过这样的事吗?

UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"galleryViewController2"];
self.window.rootViewController = vc;

您可以使用新控制器替换当前控制器,而不是推动新控制器(因为推动您需要导航控制器而崩溃)。

请注意,您不能弹出原始控制器(如果您需要返回,则需要导航)

答案 2 :(得分:0)

确保在尝试呈现任何视图控制器之前调用此方法。

[self.window makeKeyAndVisible];

答案 3 :(得分:0)

如果您需要从appDelegate导航到文件,则无法使用APP和Eclipse中的push和Pop进行导航,Segues不会提供帮助,您需要先在窗口中加载它然后再进行操作可见,如..

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

{
 NSString *screenNo =[userInfo objectForKey:@"screen"];

   UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
        firstViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:@"firstVC"];


        // First item in array is bottom of stack, last item is top.
        navController.viewControllers = [NSArray arrayWithObjects:menu, nil];

        [self.window makeKeyAndVisible];

}

答案 4 :(得分:0)

这才终于救了我。将其放在didReceiveRemoteNotification方法中。

     NSLog(@"User wanna navigate");
    [self.window makeKeyAndVisible];

    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"moreTableViewController"];
    self.window.rootViewController = vc;

    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    UIViewController *evc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"eventsViewController"];

    [navController.visibleViewController.navigationController pushViewController:evc animated:YES];