Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

前端 未结 2 1166
夕颜
夕颜 2020-12-31 11:06

I have a value in NSUserdefaults. I am using storyboard, it is embedded in UINavigationController.

- (BOOL)application         


        
相关标签:
2条回答
  • 2020-12-31 11:43

    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;
    
    0 讨论(0)
  • 2020-12-31 11:59

    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

    0 讨论(0)
提交回复
热议问题