2017-08-08 41 views
1

有誰知道一個標籤欄項是否可以有多個關係?多個關係到單個UITabbar項iOS/Objective-C?

我想能夠從單個UITabbar圖標引導到兩個不同的視圖控制器,這取決於在登錄用戶的類型。

例如,如果用戶登錄在作爲用戶類型「A 「,我希望選項卡欄圖標可以指向配置文件視圖控制器。如果用戶以用戶類型「B」登錄,我想讓相同的圖標指向設置視圖控制器。

我試圖將附加的視圖控制器連接到選項卡欄,它只是在選項卡欄上創建一個額外的圖標/選項卡。

+1

你是否試圖完成這個純粹使用故事板?或者,您是否在您的應用程序委託的applicationDidFinish啓動方法中設置了選項卡欄?另外,通過標籤欄圖標,你的意思是,在標籤上點擊,同時爲類型A或類型B的用戶顯示相同的圖標? –

回答

0

您需要從代碼中完成,所以請看setViewControllers方法。

假如你有相應vc1vc2vc A or Bvc4 4個選項卡...

你可以決定哪些VC要分配,然後實例充分與控制器的 「設置」:

// set "vcA" as the 3rd tab 
[self.tabBarController setViewControllers:@[vc1, vc2, vcA, vc4] animated:NO]; 

// or, set "vcB" as the 3rd tab 
[self.tabBarController setViewControllers:@[vc1, vc2, vcB, vc4] animated:NO]; 

或...保存在「手動」實例化控制器上...

您可以分配故事板中的所有5個控制器,然後:

// get the array of viewControllers 
NSMutableArray *a = self.tabBarController.viewControllers; 

// a now contains [vc1, vc2, vcA, vcB, vc4] 

// remove "vcA" 
[a removeObjectAtIndex:2]; 

// or, remove "vcB" 
[a removeObjectAtIndex:3]; 

// set the controllers array 
[self.tabBarController setViewControllers:a animated:NO]; 
+0

謝謝DonMag!這很好。在你的第二個解決方案中,我只需要確保self.tabBarController.viewControllers是可變的,因爲它最初是一個NSArray。我這樣做是這樣的:NSMutableArray * a = [self.tabBarController.viewControllers mutableCopy]; –

0

你也可以放置一個容器視圖在該標籤的視圖控制器,添加兩個視圖容器視圖,然後根據它是什麼類型的用戶展示期間viewDidLoad正確的觀點。

當我有時間時會添加代碼。

0

這將是這樣做的一種方法:

A.跟蹤什麼樣舉辦一個變量用戶登錄的,從以前的viewController傳遞,或在數據對象集中存放:

bool userCanAccessProfile = false; 

B.根據上述布爾,更新的佈局和相應邏輯代碼:

//layout your tab bar 
UITabBar * tabBar = [UITabBar new]; 
tabBar.frame = CGRectMake(0, h-50, w, 50); 
tabBar.delegate = self; 
[self.view addSubview:tabBar]; 

//create the item(s) 
UITabBarItem * item = [UITabBarItem new]; 
item.title = (userCanAccessProfile) ? @"Profile" : @"Settings"; 
item.image = (userCanAccessProfile) ? [UIImage imageNamed:@"profile.png"] : [UIImage imageNamed:@"settings.png"]; 
[tabBar setItems:@[item]]; 

上面那行看起來像這樣,這意味着:

something = (isThisTrue) ? (true) setThisValue : (false) setAnotherValue; 

你在問userCanAccessProfile是否爲true,如果是,那麼你就相應地設置了不同的文本和圖像。

C.當用戶點擊這個項目,你會再次查詢布爾找出該怎麼做:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { 

    //when the item is clicked 
    if (userCanAccessProfile){ 

     //open profile 

    } else { 

     //open settings 
    } 
} 

請務必設置委託在.m文件:

tabBar.delegate = self; 

並在中添加委託。h文件:

@interface yourVC : UIViewController <UITabBarDelegate>