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 thre
I too had this problem and got fed up with it. I decided to subclass UITabBarController
and override the following methods. The reason I did both was for some reason on application launch setSelectedViewController:
wasn't being called.
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
[super setSelectedIndex:selectedIndex];
// my code
}
- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
[super setSelectedViewController:selectedViewController];
// my code
}
I just dug through this tutorial on storyboards, and I thought of an alternative to using UITabBarControllerDelegate
. If you want to stick to UITabBarControllerDelegate
then feel free to ignore this answer.
First, create a subclass of UITabBarController
, let's call it MyTabBarController
. In the storyboard editor you need to change the "Class" property of the tab bar controller so that the storyboard picks up your new class.
Add this code to MyTabBarController.m
- (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SecondVC"])
{
SecondViewController* secondViewController = (SecondViewController*)segue.destinationViewController;
[secondViewController doSomething];
}
else if ([segue.identifier isEqualToString:@"ThirdVC"])
{
ThirdViewController* thirdViewController = (ThirdViewController*)segue.destinationViewController;
[thirdViewController doSomethingElse];
}
}
In the storyboard editor, you can now select the two segues that connect to SecondViewController
and ThirdViewController
and change the segue identifier to "SecondVC" and "ThirdVC", respectively.
If I am not mistaken, that's all you need to do.
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 IBOutlet
s 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 TabBarController
delegate
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 ThirdViewController
SecondViewController
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
outlets