8

In my header file I have this:

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;

}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

In my main file I have this:

@synthesize tabBarController;

-(void)viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.delegate = self;
    self.view = tabBarController.view;
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"rawr"); 
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [tabBarController release];
    [super dealloc];
}


@end

I have already connected my tabbarcontroller as a delegate to my file's owner in interface builder, but it still never calls the didSelectItem method.

Is there anything that I'm missing here?

I have already added tabBarController.delegate = self; and it still does not work.

staticbeast
  • 2,005
  • 2
  • 22
  • 24
Rowie Po
  • 1,159
  • 4
  • 12
  • 17

6 Answers6

16
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

This method is a delegate method for UITabBar, not UITabBarController, so

self.tabBarController.delegate = self;

will not work.

Tab bar controller has its own UITabBar, but changing the delegate of a tab bar managed by a tab bar controller is not allowed, so just try UITabBarControllerDelegate method like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
idearibosome
  • 554
  • 1
  • 5
  • 16
  • 4
    Don't forget to add `` to the .h of your app delegate and something like `UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self;` in the `didFinishLaunchingWithOptions` method. – Patrick Jan 30 '13 at 14:53
1

You need to add this:

tabbarcontroller.delegate = self;
Mike Weller
  • 44,483
  • 14
  • 126
  • 148
iOSDev
  • 414
  • 4
  • 10
  • tried adding: -(void)viewDidLoad{ [super viewDidLoad]; tabBarController.delegate = self; self.view = tabBarController.view; } still does not work – Rowie Po Jun 08 '12 at 09:37
1

Use UITabBarControllerDelegate instead of UITabBarDelegate and
-tabBarController:didSelectViewController{} instead of tabBar:didSelectItem{}

Interface

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarControllerDelegate>{

    IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

main file

@implementation TabBarController
    @synthesize tabBarController;

    /*other stuff*/
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        NSLog(@"rawr"); 
    }
    /*other stuff*/
@end
Sameera R.
  • 3,798
  • 1
  • 32
  • 50
0

You have synthesized the tab bar, so you now need to write: self.tabBarController.delegate = self;

Andrew Kozak
  • 1,585
  • 2
  • 20
  • 35
  • Tried adding: -(void)viewDidLoad{ [super viewDidLoad]; self.tabBarController.delegate = self; self.view = tabBarController.view; } still does not work – Rowie Po Jun 08 '12 at 09:38
0

Just set the delegate for the tab bar. You can do it by setting self.tabBarController.delegate = self; or using Interface builder do to the tabbarcontroller object (not bar) see the connection inspector and drag the connection on the file's owner.

Andrea
  • 24,774
  • 10
  • 79
  • 121
  • Probably I figured out. If you want that the method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; to be called you should set the tabbar delegate property to self. So try something like that self.tabBarController.tabBar.delegate = self; – Andrea Jun 08 '12 at 10:03
  • it does not work, if i put self.tabBarController.tabBar.delegate = self; in viewDidLoad. it gives me an error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.' – Rowie Po Jun 08 '12 at 10:08
  • I was thinking about it. here is the problem.. that tab bar you want to receive events, is managed the uitabbarviewcontroller. That means that you will never receive those messages. You can only listen to the UITabBarViewController delegate methods that indirectly come from the tab bar delegate methods. https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITabBarControllerDelegate – Andrea Jun 08 '12 at 10:22
  • I'm kinda new on this still so any suggestions that you can show how to fix this problem? It would really help. Thanks! – Rowie Po Jun 13 '12 at 03:36
-1

There are many reasons, as indicated above, why it might not be working. Here is a more subtle reason. If you are creating your UI programatically (no storyboard or Xib) you need to set the screen of your UIWindow.

self.window = [[UIWindow alloc] init];
self.window.screen = [UIScreen mainScreen];  // <- The UITabViewController will not
                                             //    accept taps without this.

If you are using a Xib, I believe this is equivalent to having "Full Screen at Launch" selected. That has to be checked or I have noticed my Tabs don't work.

Update: Whatever, down-vote me if you must but I had a non-working tab controller and this is what I had to do to get it working. None of the other answers on this page helped me. I suppose using a xib/storyboard is pretty rare these days (I think this was from iOS 5 days) but leaving here for the person that does and stumbles into this situation.

Ray Fix
  • 5,305
  • 3
  • 26
  • 30