开发者

UITabBarController Weirdness ?

开发者 https://www.devze.com 2023-03-22 21:19 出处:网络
If I normally load up a UITabBarController and switch between views, it calls the viewWillAppear of each view controller.

If I normally load up a UITabBarController and switch between views, it calls the viewWillAppear of each view controller. But it is not so.

I want to switch to another tab as soon as another tab is touched.

If I specify a tab to load up - for example [self.tabBarController setSel开发者_Go百科ectedIndex:0] in the viewWillAppear of one of the tabs (say tab 4)... It goes immediately back to tab 0.

But after that.... it does not call the viewWillAppear on any of the tabs when I switch between them. For example, if I again go to tab 4, it does not come back to tab 0. I expect it to by a never ending cycle as I expect tab 0 to load up as soon as tab 4 is touch.

But it runs JUST ONCE !!

Why ??


Note: Question has been edited.


I think I found a solution. It works every time you click on your tab and it calls viewWillAppear on both tabs.

You can do this in your AppDelegate (or somwhere else in UITabBarController's delegate):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Sample code:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if([self.tabBarController.viewControllers indexOfObject:viewController] == 1) {
        [self.tabBarController setSelectedIndex:0];
    }
}


setting the selectedIndex won't change the selectedViewController. You will have to change selectedViewController itself. Look at the documentation for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消