views:

56

answers:

1

I am trying to programmatically switch the tab bar item. The following code works fine, but I can't get it to work in certain situations:

self.tabBarController.selectedIndex = 0;

Here is the problem:

I have a tab bar controller with two UIViewController classes. In the second tab, I have the code above placed inside an if statement within the viewWillAppear method. The code above itself works, but it only works if the view has never been loaded. So, on the first tab I have a button. When that button is pushed, I programmatically switch to the second tab. At that point my code works, and I essentially stay on the first tab. But the switching doesn't work after that first time. It also doesn't work if I select that second tab before I press that button. The IF statement that my code is inside fires, but the selected tab doesn't change.

Any help would be appreciated. Please don't hesitate to ask for clarification if you need it.

Thanks!


Let me add more information:

(1) This is only used on rare occassion. The first screen is a search screen. After you press search, you are taken to the second tab automatically, unless there are no results, then you are alerted and you stay there.

(2) The second tab maintains a list of either all items, if no search to narrow down has been performed, or the the list of narrowed down items. Either way, that list is always available. So even though I stop them on the search page, if they forget and click the other tab, they will be informed there are no items. At this point I would like to bring them back to tab one, as a convience only.

It's not THAT big of a deal, I can live without it, and it's not the horrible programming scheme that some are making it out to be. I just wanted to add that one user convienence item ...


Thanks to tc! I used the following method in my UITabBarControllerDelegate:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if(self.tabBarController.selectedIndex == 0){
    if([books count] == 0){

        [self alertNoResults];

        return(FALSE);
    }
}
return(TRUE);
}

Worked like a charm!

+1  A: 

You're switching tabs during a tab switch. Ham doctor.

Try implementing UITabBarDelegate's - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; return NO if there are no results.

Alternatively, do the checking in -viewDidAppear: (it's more likely to work). If that doesn't work, you can even use performSelectorInBackground (but beware — the user might still tap other tabs!)

tc.
Doesn't view did appear only get called the first time the view is loaded?
IcyBlueRose
Thank you, that method worked great! And, as a side note, my app is now finished!!
IcyBlueRose
I think you're confusing viewDidLoad and viewDidAppear:. viewDidAppear: gets called after viewWillAppear: and any animation/set-up-etc. In particular, I suspect the tab switch hasn't happened yet in viewWillAppear:, so re-setting the tab index is going to cause problems.
tc.
Ah, you read my mind. Yes, I was confusing viewDidLoad with viewDidAppear. One of these days, these things will come naturally to me (I hope). I'm on my second app now, and it's been 1000 times easier. I think I'm actually learning my way around this language and SDK, no thanks to you and others who have helped me on this site. Thanks!
IcyBlueRose
I've ended up rewriting most of my first app after learning the "correct" way to do things (e.g. paging scroll views).
tc.