Tab click delegate button

I have two view controllers (FirstViewController and SecondViewController) and a tab bar controller, and I use storyboards. In FirstViewController, a user can drag an image. Therefore, every time the user clicks on the second TabBarItem that the SecondViewController displays, I would like to check whether the user deleted the image or not every time she clicks the TabBarItem.

Therefore, I understand that this can be done using UITabBarDelegate and with its method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item . But I am doing something wrong because the method is not being called, and I believe that this is because I cannot set the delegate correctly. Therefore, I want SecondViewController to be a 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 up the delegate, I also get a compiler warning: Assigning an "id" from an incompatible type "SecondViewController * const __strong"

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

+4
source share
6 answers

Add the following code to any of the view controllers

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

// add any delegate methods to the class

 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"%@", tabBarController); } 
+7
source
  -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; 

This method is the delegate method for UITabBar, not UITabBarController, therefore

  self.tabBarController.delegate = self; 

will not work.

The control panel controller has its own UITabBar, but changing the delegate of the tab bar controlled by the tab bar controller is unacceptable, so just try the UITabBarControllerDelegate method as follows:

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

See info for more details.

thanks

+3
source

I have imported and implemented the following. Hope this 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"); } } 
+2
source

You are using the wrong delegate protocol. UITabBarDelegate usually used to configure UITabBar objects. You need to use the UITabBarControllerDelegate protocol to check if a tab is selected or tab behavior is configured.

+1
source

Instead, you should implement the UITabBarControllerDelegate protocol and use this delegate callback to track the selection:

 tabBarController:didSelectViewController: 

Next, you must initialize the delegate before calling it. ViewDidLoad is called after the tabbarcontroller tries to talk to the delegate.

0
source

To get rid of the compiler warning, your SecondViewController must conform to the UITabBarControllerDelegate protocol instead of the UITabBarDelegate protocol.

 @interface SecondViewController : UIViewController<UITabBarControllerDelegate> 
0
source

Source: https://habr.com/ru/post/1486112/


All Articles