嗨,我正在开发一个使用标签栏的应用程序。如果用户已登录,标签栏应该有 5 个标签,但如果用户已注销,则只有 3 个。我的 MainAppDelegate.m 中有一个if
语句,如下所示,"uid"
指示用户是否已登录。
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];
然后在视图中会出现
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 个选项卡在崩溃之前显示。所以我的问题是如何在不删除更多选项卡的情况下将两个选项卡添加到中间?
感谢您的任何帮助!