嗨我正在使用一個標籤欄的應用程序。如果用戶登錄,則標籤欄應該有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個選項卡顯示出來之前它崩潰。所以我的問題是我怎樣才能將兩個標籤添加到中間而不刪除更多的選項卡?
謝謝你的幫助!