2014-12-05 117 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