Question

So I'm trying to load NotificationsViewController when the user presses a push notification. In my app, the viewController I'm trying to load has a navigation bar (contains a back button that pops back to the previous view). I have the following code implemented in my appDelegate.m

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    UINavigationController *nav = [mainStoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
    [_window setRootViewController:nav];
}
}

Now when I press the push notification, it does launch to the right view controller. However, it is without my navigation bar. How can I fix this issue? Thanks

Was it helpful?

Solution

Since the NotificationsViewController is embedded in a navigation controller, you should be instantiating it and setting it as the window's root view controller,

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  ) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
        UINavigationController *nav = [mainStoryboard instantiateViewControllerWithIdentifier:@"Nav"];
       [_window setRootViewController:nav];
    }
}

Be sure to give the navigation controller an identifier that matches the one you use here in code. When the navigation controller is made the root view controller of the window, it will display its topViewController, which is presumably your NotificationsViewController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top