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.
FirstViewController
in storyboard. TabBarController
. TabBarController
to SecondViewController
. 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.