Ios retrieved when uitabbarcontroller is selected

I need to get when the user clicks on the tabbaritem in the uitabbarcontroller in order to change something. here is my code:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if (item == [tabBarController.tabBar.items objectAtIndex:2]) { item.title = @"add shot"; } else { item.title = @"Race"; } } 

but if I do this:

 self.tabBarController.tabBar.delegate = self; 

i get sigkill ...

what is the right solution? thank you in advance

+4
source share
2 answers

Does your view controller comply with the UITabBarDelegate protocol? In the header file:

 @interface MyViewController : UIViewController<UITabBarDelegate> { // ... } - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; @end 

Then just do:

 tabBar.delegate = self; 

Instead:

 self.tabBarController.tabBar.delegate = self; 

and

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { //self.tabBarItem.title = @"Title"; } 
+6
source

I came across this answer while studying the development of iOS, but I wanted to include small missing parts for n00bs like me.

 // HelloWorldViewController.h @interface HelloWorldViewController : UIViewController <UITabBarDelegate> { } @property (weak, nonatomic) IBOutlet UITabBar *tabBar; @end 

and

 // HelloWorldViewController.m @interface HelloWorldViewController () @end @implementation HelloWorldViewController @synthesize tabBar; - (void) viewDidLoad { tabBar.delegate = self; } -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"didSelectItem: %d", item.tag); } @end 
+3
source

All Articles