whawk
whawk

Reputation: 31

didSelectViewController method not being called (with storyboard)

I have 2 versions of a tabbed ios5 application, one created using a storyboard and one using xib files. The storyboard version does not call the UITabBarControllerDelegate method didSelectViewController (the xib version does). Something is (I think) missing from the storyboard, but I don't know what. Another way of framing the question might be — how can I refer to the UITabBarController object instantiated by the storyboard?

Thanks for your help.

EDIT: The tab bar controller delegate is set:

In AppDelegate.h:

@interface MyAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
@property (strong, nonatomic) UITabBarController *tabBarController;

In AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.tabBarController.delegate = self;
    return YES;
}

Then later in AppDelegate.m, the delegate method is:

- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"Got Here");
}

The NSLog output never appears. The problem seems to me to be that I am not correctly referencing the tab bar controller object which has been instantiated by the storyboard. How do I do that?

Upvotes: 3

Views: 4254

Answers (2)

Carl Hine
Carl Hine

Reputation: 1827

I had this issue.

If you're not using storyboards, setting the UITabBarController delegate in the AppDelegate is the way to go. However, with Storyboards, the AppDelegate has no idea where the tabBarController is on startup. You'd think by subclassing the tabBarController and adding the delegate method:

(void)tabBarController:(UITabBarController *)tabBarController
   didSelectViewController:(UIViewController *)viewController {

}

... would be enough. But, it's irritatingly not.

I needed to know when a user had pressed a tab button. I needed to know this more that I needed to know that the viewController's "- (void)viewWillDisappear:(BOOL)animated {} " method had been run.

I decided to make my UITabBarController a delegate of itself. This seemed silly to me but I did the following...

#import <UIKit/UIKit.h>

@interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

And then, in my viewDidLoad method, wrote the following:

[self setDelegate:self];

Which enabled my tab bar delegate methods to run.

Crazy or what?

Ok - I'm editing this answer now, as even though the above is all correct, where a navigationController is being used, selected with each tabBarButton touched, the didSelectViewController delegate method will, when you try to NSLog(@"%@", viewController); only show you that you have selected the UINavigationController class?

So, the total solution, just to add more complexity, is to subclass the UINavigationController for each viewController that you want to monitor, (do something) when the tabBarbutton has been touched.

It works for me anyhow. And, if anyone can nit-pick through the above dribble, they might find an aspect that's useful - and that's enough for me - seeing as I find this site utterly useful too.

Upvotes: 8

Tarek Hallak
Tarek Hallak

Reputation: 18470

Put [self setDelegate:self]; in your ViewDidLoad or somewhere where the object get's initialized

Upvotes: 6

Related Questions