简体   繁体   中英

UIViewController to UITabBarController

I have a view controller named LoginViewController which is of type, UIVewController. After a user loggs in, I want to transfer that view to a UITabBarController which consists of FirstViewController and SecondViewController.

I try to do that using the following:

- (void)switchView {
    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:firstView animated:YES];
}

But it doesn't seem to work. Is it because I'm not considering the whole UITabBarController?

Yes, you should show UITabBarController:

- (void)switchView {
    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    UITabBarController *tabController = [[UITabBarController alloc] init];
    tabController.viewControllers = @[firstViewController, secondViewController];
    [self presentViewController:tabController animated:YES completion:nil];
}

After successful login, you could do something like the following and set the UITabBarController to be the new root UIViewController for your app.

In your AppDelegate.m

UITabBarController *tabController = [UITabBarController alloc] init];
[myTabBarController setViewControllers: [NSArray arrayWithObjects:firstViewController, secondViewController, nil]];

- (void)switchViewAfterSuccessfulLogin {
   if (![[[self window] rootViewController] isKindOfClass:[UITabBarController class]]){
     [self.window setRootViewController:tabController];
   }
}

Yes you should consider the UITabBarController. You could do this via the storyboard.

  1. Select the FirstViewController in storyboard.
  2. Go to Editor->Embed in->Tab Bar Controller. This would create a Tab Bar Controller TabBarController .
  3. Add a relationship segue from TabBarController to SecondViewController .
  4. Create a Push Segue from LoginViewController to TabBarController

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