2014-12-05 116 views
1

在我的iOS 7+應用程序中切換視圖控制器,我有一個4項UITabBarController。根據某些條件(myCondition),這些項目中的一個需要繼續執行viewController_A viewController_B。根據條件

我有一個TabBarController類,我已經根據myCondition設置了邏輯來更改item.image和item.title。該代碼工作正常,但我不知道該怎麼物品1發送到viewController_A viewController_B

UITabBarController *tabBarController = (UITabBarController *)self; 
UITabBar *tabBar = tabBarController.tabBar; 
UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; 
UITabBarItem *item1 = [tabBar.items objectAtIndex:1]; 
UITabBarItem *item2 = [tabBar.items objectAtIndex:2]; 
UITabBarItem *item3 = [tabBar.items objectAtIndex:3]; 

[更多一些代碼在這裏]

if (myCondition) { 
     item1.selectedImage = myItemImageSel_B; 
     item1.image= myItemImage_B; 
     item1.title= myItemTitle_B; 
    } 
    else 
    { 
     item1.selectedImage = myItemImageSel_A; 
     item1.image= myItemImage_A; 
     item1.title= myItemTitle_A; 
    } 

的塞格斯通過故事板的所有當前設置這4個項目。

我使用正確的方法嗎?或者我應該只是添加一個新項目viewController_B並隱藏它,直到我的條件爲真?

謝謝你的幫助!

回答

0

好一個方法,你能做的僅僅是設置變量值您tabview內使用switch case statement而不是使用if else condition。它也會更快。

+0

謝謝你,你是什麼設置標記值是什麼意思?這將如何確定viewController_A或viewController_B之間的切換? – DavideC 2014-12-05 11:11:44

+0

我的意思是,如果你有多個tabviews,那麼你可以設置它的標記值 – 2014-12-06 04:13:26

0

您可以使用UITabBarControllerDelegate:

tabBarController.delegate = self 

然後實現tabBarController:shouldSelectViewController:

- (void)configureTabbarItem:(UITabBarItem *)item image:(UIImage *)image selectedImage:(UIImage *)selectedImage andTitle:(NSString *)title { 
item.selectedImage = image; 
item.image = selectedImage; 
item.title = title; 
} 

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 
NSArray *items = tabBarController.tabBar.items; 
if (MyCondition) { 
    [self configureTabbarItem: items[0] 
         image: myItemImageSel_B 
       selectedImage: myItemImage_B 
        andTitle: myItemTitle_B]; 
} 
else { 
    [self configureTabbarItem: items[0] 
         image: myItemImageSel_A 
       selectedImage: myItemImage_A 
        andTitle: myItemTitle_A]; 
} 
return YES; 
} 
+0

謝謝,我的代碼工作正常,我的問題是如何根據myCondition繼續viewController_A或viewController_B,不知道你是否回答。 – DavideC 2014-12-05 11:10:20