开发者

ios retrieve when uitabbarcontroller item is selected

开发者 https://www.devze.com 2023-04-05 19:39 出处:网络
I need to retrieve when a user click over a tabbaritem into a uitabbarcontroller to change something..

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.ta开发者_如何转开发bBar.delegate = self;

i receive a sigkill...

what's the right solution? thanks in advance


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";
}


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号