2014-10-27 49 views
1

TabBar不應該啓用任何標籤,直到用戶點擊任何一個標籤, 下面是我寫的代碼來顯示標籤欄,但問題默認情況下它顯示第一個標籤。UITabBar Fuctionality

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 
self.window.backgroundColor = [UIColor whiteColor]; 
[self.window makeKeyAndVisible]; 

    PeopleViewController *peopleViewController = [[PeopleViewController alloc] init]; 
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:peopleViewController]; 
[peopleViewController setTitle:@"People"]; 

EventsViewController *eventsViewController = [[EventsViewController alloc] init]; 
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:eventsViewController]; 
[eventsViewController setTitle:@"Events"]; 

ActiveViewController *activeViewController = [[ActiveViewController alloc] init]; 
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:activeViewController]; 
[activeViewController setTitle:@"Active"]; 

MoreViewController *moreViewController = [[MoreViewController alloc] init]; 
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:moreViewController]; 
[moreViewController setTitle:@"More"]; 

NSArray *arrViewControllers = [[NSArray alloc] initWithObjects:nav1,nav2,nav3,nav4, nil]; 

[tabBarController setViewControllers:arrViewControllers]; 
[self.window addSubview:tabBarController.view]; 
    return YES; 

} 
+0

這是UITabBarController的默認行爲,你是問如何改變這種行爲? – 2014-10-27 09:29:34

+0

@Guillaume Algis是的,我想改變它。是否有可能改變這種情況?如果可能的話,請幫助我 – Madhu 2014-10-27 09:31:20

回答

0

爲標籤欄(例如theTabBar)和使用以下代碼創建一個IBOutlet:

[theTabBar setSelectedItem:nil];

+0

>應用程序崩潰,出現以下錯誤:終止應用程序,由於未捕獲異常'NSInternalInconsistencyException',原因:'直接修改標籤欄控制器管理的標籤欄不被允許。' – Madhu 2014-10-27 09:36:02

+0

回答已更新。 – 2014-10-27 09:36:39

+0

從文檔:「你永遠不應該嘗試操縱存儲在這個屬性中的UITabBar對象本身,如果你嘗試這樣做,標籤欄視圖會拋出一個異常。」 – 2014-10-27 09:38:37

0

這是UITabBarController默認行爲,默認情況下第一標籤被選中。 您可以選擇在第一個實例中選擇其他選項卡,但不能將選中的默認選項更改爲空。

[tabBarController setSelectedIndex:0]; 

默認情況下,選定索引爲0,這是第一個選項卡,您可以根據您的要求將其更改爲1,2,3。

+0

@ user3982359:有沒有其他的選擇,比如我們能否知道第二次選擇標籤項目? – Madhu 2014-10-27 09:53:27