以编程方式设置自定义UITabBarItem?

时间:2010-10-11 16:07:52

标签: iphone uitabbarcontroller

在iOS中,TabBarController中的TabBar属性是只读的。如何将自定义项与特定视图控制器关联?如何访问tabBar中的UITabBarItem?

喜欢这个

CustomView *custom = [[CustomView alloc] init];
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:@"Custom" image:[UIImage imageNamed:@"custom.png"] tag:0];
SecondView *second = [[SecondView alloc] init];
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:@"Next" image:[UIImage imageNamed:@"next.png"] tag:1];
NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
[tabBarController setViewControllers:views];
//how do I set the individual TabBarItems (customTab,secondTab) to be associated
//with the views in question?  tabBarController.tabBar is read only

1 个答案:

答案 0 :(得分:5)

在每个视图控制器中,您可以设置tabBarItem属性。如果视图控制器归UITabBarViewController所有,则标签栏上的相关项目将相应更新。

像这样的东西

-(void)viewDidLoad {
    [super viewDidLoad];
    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
    [self setTabBarItem:tbi]
    [tbi release];
}

显然,您不限制在viewDidLoad方法中执行此操作。