0

When I click the notification, I want it to open a notification viewController of the app. All codes are in AppDelegate.m

I have a function to find the topMostController

- (UIViewController *)topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

Then there's another function to display the notification viewController

    UINavigationController *navigationController = (UINavigationController *)[self topMostController];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];

    MyViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

    [navigationController pushViewController:myController animated:YES];

It works fine if the app is open. However, when the app is closed, it will crash if I click the notification. The error is

Fatal Exception: NSInvalidArgumentException
-[ViewController pushViewController:animated:]: unrecognized selector sent to instance

I think there's no topMostController or the navigationController is invalid in such. I need to use the UINavigationController because I want a "back" button to go back to a page (main page) rather than closing the app. How could I make the topMostController valid in such case? Should I create something inside didFinishLaunchingWithOptions function? Thanks.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56

1 Answers1

0

you need to assign root view controller

   self.navigationController = RootNavigationControllerViewController(rootViewController: HomeViewController.init())



    self.window?.rootViewController =  self.navigationController
    self.window?.makeKeyAndVisible()
Waqas Sultan
  • 884
  • 5
  • 13
  • Should I add this inside didFinishLaunchingWithOptions function? The HomeViewController also contains a navigationController. Probably the best way is to load the welcome page? – Will N. Walker Mar 01 '18 at 08:16
  • In that case you can simply update your rootview controller with homeview controller Thanks – Waqas Sultan Mar 05 '18 at 11:45