Reputation: 327
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 didSelectViewController
gets 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?
Upvotes: 2
Views: 2790
Reputation: 13378
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.
Upvotes: 0
Reputation: 13378
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
outletsUpvotes: 5
Reputation: 181
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
}
Upvotes: 5