1

I am trying to detect tapping of another tab from within a view controller embedded in a tabbarcontroller using one of the tabbarcontroller's delegate methods. However, I am confused about whether those methods can be in the individual view controllers or whether they have to be in the uitabbarcontroller class. I would like to have them in the view controllers where I have access to all the properties and local variables of those VCs rather than in the tabbarcontroller class.

I am also confused about how to set the delegate.

In a tableview controller embedded in the tabbarcontroller, I have declared the delegate protocol and then included the following code. However, the method is not firing. Is it okay to put this delegate method in a VC and if so, how and where should I set the delegate to get it to fire?

- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"DIDSELECTVC FIRED");
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}
Arjun
  • 89
  • 1
  • 9
  • I found it easier to make a `UITabBarController` subclass and handle the methods there. See [this answer](https://stackoverflow.com/a/47861294/3681880). – Suragch Dec 18 '17 at 02:16

2 Answers2

2

As said Toru Furuya said better way to implement UITabBarControllerDelegate is inside subclass of UITabBarController itself.

If you want to use certain inner view controller as delegate use tabBarController property:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabBarController.delegate = self;
}
  • This did the trick. it is a specialized use case. If I put the delegate methods in tabbarcontroller, then I will have to get access to a bunch of properties and logic in the individual controller so in this case, I think the unconventional use is justified. – Arjun Feb 08 '17 at 16:17
0

You can use either individual ViewController or UITabBarController itself for delegate as long as it conforms to UITabBarControllerDelegate protocol.

I think it is more common to use UITabBarController itself (or another dedicated class) to UITabBarControllerDelegate than child ViewControllers because you can set delegate only one. But if you want to use individual ViewController, I hope this code will help you.

@implementation MyTabBarController : UITabBarController

- (id)initWithCoder:(NSCoder *)aCoder{
    self = [super initWithCoder:aCoder];
    if (self) {
        MyTableViewController *controller = [[MyTableViewController alloc] init];
        controller.tabBarItem = ...
        _delegate = controller;  //Set individual ViewController to UITabBarControllerDelegate
        [self setViewControllers:@[controller] animated:YES];
    }
    return self;
}

@end

MyTableViewController is:

@interface MyTableViewController : UITableViewController<UITabBarControllerDelegate>
@end

@implementation MyTableViewController

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    //Handle tap event of UITabBarController
}
@end
Toru
  • 353
  • 3
  • 15