iOS5 setHidden UITabarItem导致崩溃
我在iOS5的xcode中使用故事板。我有一个带有6个选项卡的Tabbar控制器。在TabController之前,用户选择帐户类型a或B,如果选择类型B,我想隐藏其中一个选项卡 我有一个UITabBarController的子类,这段代码可以工作,但它不是我想要的iOS5 setHidden UITabarItem导致崩溃,ios5,storyboard,uitabbaritem,Ios5,Storyboard,Uitabbaritem,我在iOS5的xcode中使用故事板。我有一个带有6个选项卡的Tabbar控制器。在TabController之前,用户选择帐户类型a或B,如果选择类型B,我想隐藏其中一个选项卡 我有一个UITabBarController的子类,这段代码可以工作,但它不是我想要的 if (accountType == 2) { [[[[self tabBar] items] objectAtIndex:1] setEnabled:NO]; } 这使我的第二个标签变暗,无法使用,这是可以的,但我真的
if (accountType == 2) {
[[[[self tabBar] items] objectAtIndex:1] setEnabled:NO];
}
这使我的第二个标签变暗,无法使用,这是可以的,但我真的希望它能工作
[[[[self tabBar] items] objectAtIndex:1] setHidden:YES];
但它会导致此错误:-[UITabBarItem setHidden:]:无法识别的选择器发送到实例0x856f490
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UITabBarItem setHidden:]:无法识别的选择器已发送到实例0x856f490”
是否有其他方法实现此目的?为什么不等待选项卡栏ViewController的初始化,直到您知道用户选择的帐户类型?为此,请使用
setViewControllers:animated:
方法,例如:
if (accountType == 1) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5,myVC6 nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
if (accountType == 2) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5, nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
此方法的apple文档说明:
在运行时指定一组新的视图控制器时,选项卡栏
控制器在安装之前删除所有旧的视图控制器
新的。更改视图控制器时,选项卡栏
控制器会记住以前创建的视图控制器对象
选择并尝试重新选择它。如果选定的视图控制器
不再存在,它将尝试在
数组中的索引与上一个选择相同。如果该指数为
无效,它选择索引0处的视图控制器
关于您的错误消息:您得到该错误是因为选项卡栏没有实现方法
setHidden:
d。埃尼斯的回答为我指明了正确的方向。必须对ios5的情节串连板进行微调
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
if (accountType == 1) {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
} else {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
UIViewController *tvc = [storyboard instantiateViewControllerWithIdentifier:@"Third"];
}
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
NSArray *controllersForTabBar = [NSArray arrayWithObjects: fvc, svc, nil];
[tabBarController setViewControllers:controllersForTabBar animated:NO];
[self.view addSubview:tabBarController.view];