当选择uitabbarcontroller项时,ios将检索

时间:2011-09-19 22:02:21

标签: objective-c ios uitabbarcontroller uitabbar uitabbaritem

当用户点击tabbaritem进入uitabbarcontroller进行更改时,我需要检索... 这是我的代码:

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

但如果我这样做:

self.tabBarController.tabBar.delegate = self;

我收到一个sigkill ......

什么是正确的解决方案?提前谢谢

2 个答案:

答案 0 :(得分:6)

您的视图控制器是否符合UITabBarDelegate协议? 在头文件中:

@interface MyViewController : UIViewController<UITabBarDelegate> {
    // ...
}

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

@end

然后,就这样做:

tabBar.delegate = self;

而不是:

self.tabBarController.tabBar.delegate = self;

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

答案 1 :(得分:3)

我在学习iOS开发时遇到了这个答案,但是我想要像我这样的n00bs包含一些缺失的小部分。

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

// 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