4

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 setSelectedIndex: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.

Legolas
  • 12,145
  • 12
  • 79
  • 132
  • 1
    This question does not need any code. I would be happy to post code but explaining with a code will make the question TOO long. The question is straight forward. – Legolas Jul 26 '11 at 18:44
  • OK. So where do you call `setSelectedItem`? In some static method (e.g. `applicationDidFinichLaunching`) or somwhere else? – akashivskyy Jul 26 '11 at 18:48
  • Actually you brought up a good point. Let me edit the question to my exact scenario – Legolas Jul 26 '11 at 18:55
  • Have you checked the `viewDidDisappear:` methods for each view? Though I doubt it's the case, the views may not know they have disappeared and thus can't appear again. If nothing else, it could be worth checking – justin Jul 26 '11 at 19:12
  • I can reproduce only halp of this. I've set up two tabs and if I click on the second I call `setSelectedIndex:0` and it shwitches to 0 calling `viewWillAppear` in it. But it runs just once... Are you sure that `viewWillAppear` on your '0' tab is not called? – akashivskyy Jul 26 '11 at 19:14
  • @Kashiv: Yes, ofcourse. That is why I am posting the question here. I tried NSLogging at various places, and I still have the problem. – Legolas Jul 26 '11 at 19:21

2 Answers2

2

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];
    }
}
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • This approach will lead to the situations when **viewWillAppear** will be called twice (native by user interaction and sec on by this code). If there is no critical code inside **viewWillAppear** than no problem. – Serhii Mamontov Aug 04 '11 at 15:18
  • You can always create a property indicating that the critical code should be executed or not – akashivskyy Aug 05 '11 at 06:00
0

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

yinkou
  • 5,756
  • 2
  • 24
  • 40
  • I change my selected tabs with `setSelectedIndex` and it works, so it's not that... – akashivskyy Jul 26 '11 at 18:58
  • setting setSelectedIndex just changes the selected tab, but it doesn't handle it like a touch on the specified item. You will **need** to set selectedViewController. – yinkou Jul 26 '11 at 19:24
  • No, he doesn't. `setSelectedIndex != setSelectedTab`. It automatically sets `selectedViewControler`. – akashivskyy Jul 27 '11 at 09:13
  • @Legolas I think what @yinkou is right. Try to do something like this: `[[self tabBarController] setSelectedViewController:[[[self tabBarController] viewControllers] objectAtIndex:0]]` – Serhii Mamontov Aug 04 '11 at 15:24
  • Using `-selectedIndex:` will have no effect if the More nav controller is in use and the indexed tab effectively lives within it. In those cases, `-selectedViewController:` is the way to do it, per the docs. – Joe D'Andrea Mar 02 '12 at 15:40