简体   繁体   中英

How do you change the initial view controller in an ios application based on a remote notification?

I have an application that is implementing remote notifications. My desire is to change the initial view controller based on one of three conditions: the user started the app on their own (no notifications); or, the user received an suspicious activity alert; or, the user received a crime alert. I have implemented the application didReceiveRemoteNotification: method in the appDelegate. Based on the notification and the user response to the notification I have implemented the following code:

UITabBarController *tbContr = (UITabBarController*) self.window.rootViewController;
UINavigationController *navContr = [tbContr.viewControllers][2];
ViewCrimesController *viewCrimes = [navContr.storyboard instantiateViewControllerWithIdentifier:@"ViewCrimes"];
[navContr presentViewController:viewCrimes animated:YES completion:nil];

[self.window.makeKeyAndVisible];

The problem I am having is that the navigation controls -- ie, the back button and the navigation bar title; are not on the ViewCrimesController when it is presented. I have tried to load the ViewCrimesController many different ways. Each way I get either and error saying there is no segue (this view is a model view to map view) or I am attempting to load the over an active view or, again, I don't get the navigation controls.

Do I need to specifically program the navigation controls or am I missing something in the way I am attempting to load the view?

I have seen references to dynamically changing the initial view in other posts. But I have not seen anything that indicates specific programming is required to add the controls. Any help you can provide is greatly appreciated!

Susan

Have you looked at the post, Programmatically set the initial view controller using Storyboards

The answer from that post may solve your issue,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

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

    UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

   self.window.rootViewController = viewController;
   [self.window makeKeyAndVisible];

   return YES;
}

You can also download this sample, and implement it as per your requirement.

simply change the key window's rootViewController, sample code placed here, hope it helps:

UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
    self.window.rootViewController = rootViewController;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM