Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

血红的双手。 提交于 2019-11-30 09:09:52

You can create an object of UINavigationController with your ViewController as per if/else condition and set the navigation controller as rootViewController property of window in AppDelegate as follows:

LoginViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
Prince Agrawal

This is what i used in my code

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // Get user preference
 NSString * wxyz=[defaults stringForKey:@"wxyz"];
    //Get value at wxyz field

if ([self isInValidwxyz:wxyz]) {
    //check if wxyz is invalid
    //If wxyz is invalid, write custom code
}
else{
    //if valid, 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryBoardiPhone" bundle:nil];
        self.window.rootViewController = [storyboard instantiateInitialViewController];
    }
    else{

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryBoardiPad" bundle:nil];
        self.window.rootViewController = [storyboard instantiateInitialViewController];;
    }
    [self.window makeKeyAndVisible];
}

and then, go through this link for implementation with navigation controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!