3

Here is my code stub for my app-delegate.m -- it never gets called.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%s", __FUNCTION__);
}

It is defined in this app-delegate.h

@interface OrioleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}

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

@end
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
mobibob
  • 8,670
  • 20
  • 82
  • 131

3 Answers3

15

If your ViewController is a UITabBarController, you need to set self as it's delegate because you can't change the delegate of the UITabBar directly.

For example, in the ViewDidLoad of your UITabBarController :

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
}
CedricSoubrie
  • 6,657
  • 2
  • 39
  • 44
  • ya its working for me too... actually I had tried it before and then search for this to check is it correct way or not and I got your answer so ... – Maulik Apr 23 '13 at 06:40
  • This is working for me as well. I'm using this in an extended UITabBarController class, and in my case I initially got a compiler Warning saying: "Assigning to 'id' from incompatible type 'ECMainTabBarViewController *const __strong'". I fixed that by casting the delegate to type (id), as follows: self.delegate = (id)self; – Erik van der Neut Sep 11 '14 at 03:02
15

Did you make a connection between your UITabBarController and your application delegate?

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
     ...
     tabBarController.delegate = self;
     ...
}
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • Yup -- I forgot and you were too quick :)) Thanks for the super-light-speed response. – mobibob Mar 27 '10 at 17:57
  • Hell, I tried, this solution and didn't work. Are either of you abel to help? I put my code here: Hello, i am have a similar problem. I've posted my code here: http://stackoverflow.com/questions/10891182/iphone-how-to-implement-didselectvie‌​wcontroller/10891264. – John Jun 05 '12 at 17:41
0

I added the following tabBarController.delegate = self; and all is well. I hope this is helpful to others.

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    tabBarController.delegate = self;
    [window addSubview:tabBarController.view];
}
mobibob
  • 8,670
  • 20
  • 82
  • 131
  • I am facing the same problem, I made sure that I have delegate set up as: tabBarController.delegate = self; but still the delegate methods are not being called. – Vibhor Goyal Oct 27 '10 at 22:34
  • check for conflicts from your IB definition. what class are you defining in the builder and is it hooked up to your cod correctly? – mobibob Oct 28 '10 at 03:23