ios: How open Tab Bar controller from ViewController using StoryBoards

后端 未结 3 1616
失恋的感觉
失恋的感觉 2021-01-05 09:09

I am doing this and want to open Tab bar Controller from Login Page if its first time and if already login then pass the LoginPage and open Tab Bar Controller after Spalsh

相关标签:
3条回答
  • 2021-01-05 09:38

    It seems that TripMapViewer is the Storyboard ID of some Tab and not the UITabBarController , please make it sure and it will work

    as in the below code MainTabBar is Storyboard ID of UITabBarController and it works perfectly

    UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabBar"];
    tbc.selectedIndex=0;
    [self presentViewController:tbc animated:YES completion:nil];
    
    0 讨论(0)
  • 2021-01-05 09:40

    You don't need a splash screen controller unless you are animating something. This example uses NSUserDefaults to remember if it's first login or not.

    In you application delegate put the following:

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
       {
    
    
            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
            if(![[NSUserDefaults standardUserDefaults] dictionaryForKey:@"someKey"]){
                UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"registerViewController"];
                self.window.rootViewController = viewController;
            } else {
                UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"mainViewController"];
                self.window.rootViewController = viewController;
            }
    
            [self.window makeKeyAndVisible];
    
    
    
             return YES;
      }
    

    If you decide you must have a viewcontroller for the splash screen then you can put the same code there.

    0 讨论(0)
  • 2021-01-05 09:44

    The fact that the VC being instantiated is not a TabBarController explains the crash. The code asks storyboard for a regular vc, casts it as a UITabBarController, then sends it a message setSelectedIndex: that it doesn't implement because it's not a tab bar controller.

    At least the first step in getting where you want to go is to add an identity in storyboard to the tab bar controller (the for which TripMapViewer is one of the tabs). Then instantiate that, and the crash should be gone.

    At the design level, please see my question and answer here about how to do login and splash.

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