Question

I am trying to change from the App delegate method to the Master View when I receive a Remote Notification, in order to perform a segue in the Master View to another view, but I am getting an NSInvalidArgumentException

Code in App Delegate when didReceiveRemoteNotification:

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

    EmergencyMasterViewController* maincontroller = (EmergencyMasterViewController*)self.window.rootViewController;
    [maincontroller alert];
}

Code in MasterView:

-(void)alert
{
    [self performSegueWithIdentifier: @"Warning" sender: self];
}

And the error I am getting: [UINavigationController alert]: unrecognized selector

Was it helpful?

Solution

It is because your window rootViewController is actually a UINavigationController instead of your EmergencyMasterViewController. You need to check how you assign the window root view controller in your app delegate didFinishLaunchingWithOptions or something similar.

Try to get the view controller embedded in the navigation controller, for example:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 
NSArray *viewControllers = navigationController.viewControllers 
EmergencyMasterViewController *maincontroller = [viewControllers objectAtIndex:0];

It might be safer for the UINavigationController to pop to root view controller first before you try to get the EmergencyMasterViewController, in case the user is already navigating his way through the navigation stack:

[navigationController popToRootViewControllerAnimated:NO];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top