【问题标题】:Xcode Storyboard Tabbed application (UITabBarController) - tell when tab selectedXcode Storyboard 选项卡式应用程序(UITabBarController) - 告知何时选择了选项卡
【发布时间】:2015-01-31 18:42:47
【问题描述】:

使用 Xcode 6,我使用带有提供的模板的情节提要创建了一个选项卡式应用程序。

当 UITabBarController 的第三个选项卡被选中时,我需要一个函数来触发。

我不能使用 ViewDidLoad,因为每次通过单击选项卡访问视图时都需要它触发(不仅仅是第一次(而且我不能使用 ViewWillAppear,因为我需要通过以下方式访问视图时的特定行为)选项卡,而不是从后续(模态)视图控制器中分离出来。

任何建议将不胜感激。非常感谢。

【问题讨论】:

    标签: ios xcode xcode6 uitabbarcontroller tabbed-interface


    【解决方案1】:

    在一些 UIViewController 类上实现这个 UITabBarControllerDelegate 的委托方法

    - (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    // Your code here
        }
    

    或

    您可以继承 UITabBarController 并覆盖以下方法。

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
    // Your code here}
    

    【讨论】:

    • 感谢您的快速回复。我应该在哪里实现该功能 - 在应用程序委托中?
    • @CarlGilbert 我已经为你更新了我的答案,如果有帮助请告诉我。
    • 尝试在应用委托中实现,但没有被调用
    • 您是否将其委托设置为自我?
    • 如果您对委托方式有疑问,请尝试第二种方式;只需将您的 tabbarcontroller 子类化并覆盖答案中的方法。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2012-05-12
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多