嗨我正在使用一个标签栏的应用程序。如果用户登录,则标签栏应该有5个选项卡,但如果用户登出,则标签栏应该只有3个。我在我的MainAppDelegate.m if语句看起来像这样在那里,如果用户登录或UID告诉不IOS标签栏动态标签
UIViewController *popular = [[[PopularViewController alloc] initWithNibName:@"PopularViewController" bundle:nil] autorelease];
UIViewController *upcoming = [[[UpcomingViewController alloc] initWithNibName:@"UpcomingViewController" bundle:nil] autorelease];
UIViewController *account = [[[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil] autorelease];
UIViewController *message = [[[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil] autorelease];
UIViewController *more = [[[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([prefs objectForKey:@"uid"]){
self.tabBarController.viewControllers = @[popular, upcoming, account, message, more];
}else{
self.tabBarController.viewControllers = @[popular, upcoming, more];
}
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
当用户登出我做它可以通过消除客户带我到三个选项卡下面和消息如果用户在
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
[tbViewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:tbViewControllers];
记录现在更多的网页上只应被看作是登录,这样,如果他们按他们被带到一个新的视图控制器登录。如果他们成功登录我做以下
回去标签视图
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
然后在viewWillAppear中
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([prefs objectForKey:@"uid"]){
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
UIViewController *account = [[[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil] autorelease];
UIViewController *message = [[[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil] autorelease];
UIViewController *more = [[[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil] autorelease];
[tbViewControllers addObject:account];
[tbViewControllers addObject:message];
[tbViewControllers addObject:more];
[self.tabBarController setViewControllers:tbViewControllers];
}
的问题是,他们更多的页面已经这样除去沉在对象上索引2将删除更多页面,从而导致应用程序崩溃,即使我看到5个选项卡显示出来之前它崩溃。所以我的问题是我怎样才能将两个标签添加到中间而不删除更多的选项卡?
谢谢你的帮助!