Changing tabBarController.selectedViewController only works once?
My iPhone app uses a UITabBarController. On the first tab, there's a scenerio where the user can go two UIViewControllers deep without changing tabs. So the screenflow is like this:
- App loads with first tab opened by default.
- User presse开发者_JAVA百科s a UIButton which takes them to second UIViewController in the same tab.
- From the second UIViewController, user can press another UIButton taking them to a third UIViewController, still in the same tab
This works when switching from the first UIViewController to the second with these 2 lines:
2ndViewController = [[SecondViewController alloc] init];
self.tabBarController.selectedViewController = 2ndViewController;
However, when I try to do the same from the second UIViewController to the third, it doesn't work. The button doesn't do anything, and I stay on the second UIViewController.
Can anyone explain why that is? Is there a workaround I can use? I'm happy to provide more code if needed.
Thanks in advance.
This isn't what that property was meant for. You are most likely experiencing the "undefined behavior" the documentation talks about.
The best way to do what you are doing is to place a UINavigationController
in the tab and push each controller with that. If you don't want to show the navigation bar, you can hide that with navigationBarHidden
.
精彩评论