Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios retrieve when uitabbarcontroller item is selected

I need to retrieve when a user click over a tabbaritem into a uitabbarcontroller 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 receive a sigkill...

what's the right solution? thanks in advance

like image 530
ghiboz Avatar asked Jan 18 '23 17:01

ghiboz


2 Answers

Does your view controller conform to 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 of:

self.tabBarController.tabBar.delegate = self;

And:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    //self.tabBarItem.title = @"Title";
}
like image 84
chown Avatar answered Jan 29 '23 03:01

chown


I came across this answer while learning iOS development, But I wanted to include the little missing pieces 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
like image 32
Alex Nolasco Avatar answered Jan 29 '23 03:01

Alex Nolasco