3

I want to catch the event when someone switches between tabs. I have the following two function in my appdelegate file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController * uitbc = [storyboard instantiateViewControllerWithIdentifier:@"tabbarcontroller"];
    uitbc.delegate = self;
    [self.window addSubview:uitbc.view];

    return YES;
}


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

But the NSLog(@"switching"); never fires. The xcode issues a warning for the line uitbc.delegate = self; saying "Passing appdelegate const__strong to parameter of incompatible type id".

What am I doing wrong? I'm just following the accepted answer found here, except i'm instantiating my tabbarcontroller form story board:

how to get the event that switch tab menu on iphone

Update Based on skram's suggestion, I wrote this for my appdelegate but the NSLOG(Switching) still doesn't fire:

@interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>

I also updated my didFinishLauchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = self.window.rootViewController.tabBarController;
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}

Good thing is that nothing crashes. I also no longer et the warning about incompatible types. But still, didSelectViewController doesn't fire.

Community
  • 1
  • 1
John
  • 32,403
  • 80
  • 251
  • 422

3 Answers3

4

in my appdelegate.h file, I changed the line

@interface wscAppDelegate : UIResponder <UIApplicationDelegate>

to

@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>

Then in my CustomTabBarController in the viewDidLoad function i added these lines:

wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;

Then in appdelegate.m file, I added this function

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

NSLog(@"hooray this works");

}
John
  • 32,403
  • 80
  • 251
  • 422
2

I also struggled with this a lot but my UITabBarNavigationController is pretty far from AppDelegate to set it there, and the only way to actually make it work was to do it not in the viewDidLoad of the UITabBarController subclass itself, but in its:

-(void)viewDidLayoutSubviews {
    [ super viewDidLayoutSubviews ];
    self.delegate = self;
}

And that solved it. Alternatively you can do it in any of the tab bar controlled viewcontrollers, but that just feels wrong.

maksa
  • 374
  • 5
  • 16
0

You need your AppDelegate to conform to UITabBarControllerDelegate protocol. See below..

@interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>
skram
  • 5,314
  • 1
  • 22
  • 26
  • Still not firing. I updated my question to reflect this. Any other ideas? Thanks – John Jun 05 '12 at 12:07
  • do you still get the warning? – skram Jun 05 '12 at 12:42
  • The warning is gone so that's good. Maybe i still haven't established some relationships here and there between my app delegate and the correct tabbarcontroller? – John Jun 05 '12 at 13:35