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

Jus*_*tin 2 iphone uitabbarcontroller

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

像这样

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
Run Code Online (Sandbox Code Playgroud)

sig*_*egv 5

在每个视图控制器中,您可以设置tabBarItem属性.如果视图控制器由UITabBarViewController拥有,则选项卡栏上的相关项将相应更新.

像这样的东西

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

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