【问题标题】:How to detect tab bar index changes in swift?如何快速检测标签栏索引的变化?
【发布时间】:2018-05-31 20:47:06
【问题描述】:

我在标签栏控制器中有 3 个标签:Main、TabOne、TabTwo(标签索引分别为 0、1、2)

主视图控制器有一个表格视图,将显示一个数组中的所有元素。在视图控制器 TabOne 和 TabTwo 中,它们都有按钮:

@IBAction func mypost(_ sender: Any) {
    tabBarController?.selectedIndex = 0}

主视图控制器中的数组应根据前一个视图控制器的选项卡索引进行不同的更改。前任。 如果前一个视图控制器是 TabTwo,那么表格视图将只显示偶数索引的所有元素 但是 如果之前的视图控制器是 TabOne,那么表格视图将只显示奇数索引的所有元素

我该怎么做?

【问题讨论】:

  • 你的问题不是很清楚。你到底有什么问题?
  • 我建议重新考虑你的界面。

标签: swift uitabbarcontroller tabbar


【解决方案1】:

一种可能的解决方案是将您的主视图控制器设置为标签栏控制器的代理。然后实现委托方法shouldSelect,查看标签控制器当前选中的索引。

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
    let index = tabBarController.selectedIndex
    if index == 1 {
        // load data appropriate for coming from the 2nd tab
    } else index == 2 {
        // load data appropriate for coming from the 3rd tab
    }

    return true
}

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 2017-12-27
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多