4

I have two view controllers (FirstViewController and SecondViewController) and a Tab Bar Controller and I'm using Storyboards. In the FirstViewController user can drag and drop an imageview. So every time a user clicks on the second TabBarItem which displays the SecondViewController I would like to check if the user has dropped the image or not every time she clicks the TabBarItem.

So I understand that this can be done with UITabBarDelegate and with its method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item. But I'm doing something wrong because the method isn't called and I believe this is because I can't set the delegate properly. So I want the SecondViewController to be the delegate for TabBarController.

So in my SecondViewController.h I have the following

@interface SecondViewController : UIViewController<UITabBarDelegate>

And in SecondViewController.m I have

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

}

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

But nothing happens and when setting the delegate I also get a compiler warning: Assigning to 'id' from incompatible type 'SecondViewController *const __strong'

Please be gentle with me, this is my first app and the first time I'm trying to use delegates.

BenMorel
  • 30,280
  • 40
  • 163
  • 285
flouwer
  • 227
  • 1
  • 3
  • 12
  • For detecting taps in Swift with a `UITabBarController`, see [this question](https://stackoverflow.com/questions/30849030/swift-how-to-execute-an-action-when-uitabbaritem-is-pressed) – Suragch Dec 18 '17 at 02:15

6 Answers6

8

Add the following code to any of the view controllers

UITabBarController *tabBarController = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;

    [tabBarController setDelegate:self];

// add any delegates methods to your class

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%@", tabBarController);
}
Shams Ahmed
  • 4,452
  • 4
  • 19
  • 27
  • This made it work, also changed the delegate to UITabBarControllerDelegate as many of you pointed out. Just wondering why self.tabBarController.delegate = self doesn't work? Also in what situations can I use UITabBarDelegate? – flouwer Jun 14 '13 at 05:46
  • The tabBarConttroller only exist in your appDelegate, when your view your storyboard viewController1 and viewController2 doesn't have that object. when your call self.tabBarController is doesn't exist as its looking in the calling class. The UITabBarDelegate class provides the ability for the user to reorder, remove, and add items to the tab bar only – Shams Ahmed Jun 14 '13 at 07:21
  • Thank you very much! Really appreciate it! – flouwer Jun 14 '13 at 10:32
3
 -(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
NSLog(@"%@", item);

}

For more detail check info

Thanks

Community
  • 1
  • 1
jamil
  • 2,409
  • 3
  • 34
  • 64
2

I imported and implemented the following. Hope it helps.

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

    if (_mainTab.selectedItem.tag == 1) {
        NSLog(@"TAB 1");
    }

else if (_mainTab.selectedItem.tag == 2) {

        NSLog(@"TAB2");

    }

else if (_mainTab.selectedItem.tag == 3)
    {
        NSLog(@"TAB3");
    }

else
    {
        NSLog(@"TAB NOT WORKING");
    }

}
Robert
  • 5,191
  • 43
  • 59
  • 113
alvin
  • 21
  • 1
1

You are using the wrong delegate protocol UITabBarDelegate is usually used for customizing the UITabBar objects. You need to use UITabBarControllerDelegate protocol in order to check if a tab is selected or customize the behavior of tabs.

danypata
  • 8,829
  • 1
  • 28
  • 41
0

You should implement UITabBarControllerDelegate protocol instead and use this delegates callback to track selection:

tabBarController:didSelectViewController:

Next thing is, that you should initialize delegate before it will be called. ViewDidLoad will be called after tabbarcontroller will try to talk to delegate.

Andrei Shender
  • 2,487
  • 20
  • 14
  • First, `tabBarController:didSelectViewController:` is a method of `tabBarController:didSelectViewController:` and if you don't implement this method you get a different warning. Second, the warning that the SO is talking about is related to delegate's wrong type. – danypata Jun 13 '13 at 21:32
0

In order to get rid of the compiler warning your SecondViewController should conform to the UITabBarControllerDelegate protocol instead of the UITabBarDelegate protocol.

@interface SecondViewController : UIViewController<UITabBarControllerDelegate>
riik
  • 4,368
  • 1
  • 10
  • 16