【问题标题】:Switching view controller from a tabbar item according to condition根据条件从标签栏项目切换视图控制器
【发布时间】:2015-02-03 01:45:56
【问题描述】:

在我的 iOS 7+ 应用程序中,我有一个 4 项 UITabBarController。根据某些条件 (myCondition),其中一项需要与 viewController_A viewController_B 相匹配。

我有一个 TabBarController 类,我已经根据 myCondition 设置了逻辑来更改 item.image 和 item.title。代码工作正常,但我不知道如何将 item1 发送到 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 添加一个新项目并将其隐藏直到我的条件为真?

感谢您的帮助!

【问题讨论】:

    标签: objective-c segue tabbar


    【解决方案1】:

    您可以做的一种方法是在tabview 中设置标签值并使用switch case statement 而不是使用if else condition。而且会更快。

    【讨论】:

    • 谢谢,设置标签值是什么意思?这将如何确定 viewController_A 或 viewController_B 之间的切换?
    • 我的意思是如果你有多个tabviews那么你可以设置它的标签值
    【解决方案2】:

    你可以使用 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;
    }
    

    【讨论】:

    • 谢谢,我的代码运行良好,我的问题是如何根据 myCondition 切换到 viewController_A 或 viewController_B,不确定您是否回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2015-09-08
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多