I have the problem that many already have reported, didSelectViewController doesn't get called, but in my case it sometimes gets called. I have three tabs and three view controllers. Every time user presses second or third tab I need to execute some code. In my SecondViewController and ThirdViewController I have:
UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
[tabBarController setDelegate:self];
Now everything works fine with the SecondViewController, the didSelectViewController gets called every time the second tab is pressed. Also in ThirdViewController didSelectViewControllergets called every time the third tab is pressed but only when second bar is meanwhile not pressed. So when I switch back and forth between FirstViewController and ThirdViewController everything is OK. But when I go in a pattern like first->second->third, then didSelectViewController doesn't get called in ThirdViewController. Also when I go like first->third->second->third didSelectViewController gets called in ThirdViewController the first time but not the second time. Any ideas?
It's hard to follow what exactly you are doing, but from what I understand you are responding to tab switches by changing the UITabBarController's delegate back and forth between SecondViewController and ThirdViewController.
If that is true, I would advise against doing this. Instead I would suggest you try the following:
tabBarController: didSelectViewController:.SecondViewController and ThirdViewController instances. If you are designing your UI with Interface Builder, you might do this by adding two IBOutlets to the delegate class and connecting the appropriate instances to the outlets.tabBarController: didSelectViewController: it can simply forward the notification to either SecondViewController or ThirdViewController, depending on which of the tabs was selected.A basic code example:
// TabBarControllerDelegate.h file
@interface TabBarControllerDelegate : NSObject <UITabBarControllerDelegate>
{
}
@property(nonatomic, retain) IBOutlet SecondViewController* secondViewController;
@property(nonatomic, retain) IBOutlet ThirdViewController* thirdViewController;
// TabBarControllerDelegate.m file
- (void) tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{
if (viewController == self.secondViewController)
[self.secondViewController doSomething];
else if (viewController == self.thirdViewController)
[self.thirdViewController doSomethingElse];
}
EDIT
Some hints on how to integrate the example code from above into your project:
TabBarControllerDelegate to the .xib file that also contains the TabBarControllerdelegate outlet of TabBarController' to the TabBarControllerDelegate instancesecondViewController outlet of TabBarControllerDelegate to the SecondViewController instancethirdViewController outlet of TabBarControllerDelegate to the ThirdViewController instance- (void) doSomething to SecondViewController- (void) doSomethingElse to ThirdViewControllerSecondViewController and ThirdViewController changes the TabBarController delegate!Once you are all set and everything is working fine, you will probably want to cleanup a bit:
doSomething and doSomethingElse to something more sensiblesecondViewController and thirdViewController outletsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With