gpt4 book ai didi

swift - 如何快速检测标签栏索引变化?

转载 作者:行者123 更新时间:2023-11-30 11:27:02 25 4
gpt4 key购买 nike

我在选项卡栏 Controller 中有 3 个选项卡:Main、TabOne、TabTwo(分别为选项卡索引 0、1、2)

主视图 Controller 有一个 TableView ,将显示数组中的所有元素。在 View Controller TabOne 和 TabTwo 中,它们都有按钮:

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

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

我该怎么做?

最佳答案

一种可能的解决方案是将主视图 Controller 设置为选项卡栏 Controller 的委托(delegate)。然后实现委托(delegate)方法 shouldSelect 并查看选项卡 Controller 当前选定的索引。

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
}

关于swift - 如何快速检测标签栏索引变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50632532/

25 4 0