Programmatically set the initial view controller using Storyboards

前端 未结 22 2220
感情败类
感情败类 2020-11-22 15:00

How do I programmatically set the InitialViewController for a Storyboard? I want to open my storyboard to a different view depending on some condition which may

22条回答
  •  [愿得一人]
    2020-11-22 15:15

    You can set Navigation rootviewcontroller as a main view controller. This idea can use for auto login as per application requirement.

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    
    UIViewController viewController = (HomeController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"HomeController"];
    
    UINavigationController navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    
     self.window.rootViewController = navController;
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
    
        // do stuff for iOS 7 and newer
    
        navController.navigationBar.barTintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0];
    
        navController.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0];
    
        navController.navigationBar.tintColor = [UIColor whiteColor];
    
        navController.navigationItem.titleView.tintColor = [UIColor whiteColor];
    
        NSDictionary *titleAttributes =@{
    
                                         NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
    
                                         NSForegroundColorAttributeName : [UIColor whiteColor]
    
                                         };
    
        navController.navigationBar.titleTextAttributes = titleAttributes;
    
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    }
    
    else {
    
        // do stuff for older versions than iOS 7
    
        navController.navigationBar.tintColor = [UIColor colorWithRed:88/255.0 green:164/255.0 blue:73/255.0 alpha:1.0];
    
    
    
        navController.navigationItem.titleView.tintColor = [UIColor whiteColor];
    
    }
    
    [self.window makeKeyAndVisible];
    

    For StoryboardSegue Users

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    
    // Go to Login Screen of story board with Identifier name : LoginViewController_Identifier
    
    LoginViewController *loginViewController = (LoginViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@“LoginViewController_Identifier”];
    
    navigationController = [[UINavigationController alloc] initWithRootViewController:testViewController];
    
    self.window.rootViewController = navigationController;
    
    [self.window makeKeyAndVisible];
    
    // Go To Main screen if you are already Logged In Just check your saving credential here
    
    if([SavedpreferenceForLogin] > 0){
        [loginViewController performSegueWithIdentifier:@"mainview_action" sender:nil];
    }
    

    Thanks

提交回复
热议问题