2

I have created a custom UITabBarController by using Martin's tutorial. My subclass FSTabBarController switches between view controllers, and acts normal as far as I can see.

The issue is, when I change my tabBarContoller to my subclass, It won't respond to my delegate;

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

If I change it back to UITabBarController -when I use the default UITabBarController- the delegates works as it should.

The custom subclass uses the below function to represent tab selection:

- (void)_buttonClicked:(id)sender
{
    self.selectedIndex = [sender tag];
    [self _updateTabImage];
}

Edit:

AppDelegate.h

...
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) FSTabBarController *tabBarController;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
    self.tabBarController = [[FSTabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:peopleViewController,viewController,profileViewController, nil];
    self.tabBarController.delegate = self;
...
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   // not called when FSTabBarController, called when UITabBarController !!
}
Bartu
  • 2,189
  • 2
  • 26
  • 50

1 Answers1

3

OK, downloaded the sample from his site and tested. Yes you need to manually call the deleage from the subclass:

this is how you should change the buttonClicked function:

- (void)_buttonClicked:(id)sender
{
    self.selectedIndex = [sender tag];
    if (self.delegate) {
        [self.delegate tabBarController:self didSelectViewController:self.selectedViewController];
    }
    [self _updateTabImage];
}
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • thanks alot... This means I need to track down all other delegates I want to implement and call them myself within my subclass... discarding all views of UITabBarController doesn't seems to be a good way to make custom one after all... – Bartu Sep 23 '12 at 10:22
  • It is because of the way the custom class is implemented. Instead of adding the buttons as normal TabBar buttons, and relaying on the TabBarController, he is actually adding UIButtons on top. I would suggest you just subclass the tabbarcontroller in a different way, so you can benefit from the tabbarcontroller delegate methods. The way is described [here](http://stackoverflow.com/a/3762625/312312) – Lefteris Sep 23 '12 at 10:56