user3296042
user3296042

Reputation: 65

Navigating from Appdelegate when push message received

Currently this is how my method loooks

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

}

Based on the screenNo I would like to navigate to different view controllers. But I couldn't do as most of the answers given below.

Reason is that my root view is not navigation control, so I couldn't segue. It crashes the app.

when push message arrives didReceiveRemoteNotification is called and I could see the content of the message too. But it doesn't get navigated using the methods shown here.

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

this is the exception

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

Thanks for your time in advance.

Upvotes: 0

Views: 522

Answers (5)

user3296042
user3296042

Reputation: 65

This is what finally saved me. placed it inside didReceiveRemoteNotification method.

     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];

Upvotes: 0

Víctor B.
Víctor B.

Reputation: 1680

Did you try something like this?

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

Instead of pushing your new controller (it crashes because to push you need a navigation controller) you can replace current controller with the new one.

Please, take in account that you can not pop to the original controller (if you need to get back, you need a navigation)

Upvotes: 1

Geet
Geet

Reputation: 2437

You can't navigate using push and Pop from APPDelegate if you need to navigate from appDelegate to a file, Segues won't help either then You would need to load it first in your window and then make it Visible such as..

- (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];

}

Upvotes: 0

Casper
Casper

Reputation: 419

You're trying to push a UIViewController with a UIViewController. This is not possible. You must have a UINavigationController in your app hierarchy. You can also just set the rootViewController:

[self.window setRootViewController: newViewController];

Upvotes: 1

Farai
Farai

Reputation: 190

make sure you call this method before trying to present any view controller.

[self.window makeKeyAndVisible];

Upvotes: 0

Related Questions