我有一个 UITabBarController
的子类。问题是:有没有办法检查用户是否选择了已经选择的 UITabBarItem
?
我试过了
didSet
用于 selectedIndex
和 selectedViewController
以及不同的委托(delegate)方法:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
但只有当它是不同的选项卡时它们才会被调用(并且 selectedIndex
根本不会被调用)
我需要它,例如,如果我想将当前打开的 Controller 滚动到顶部,如果它不是当前打开的 Controller ,那么我不想滚动它,我只想显示它(还有其他一些与动画相关的东西)。
谢谢
编辑
我的代码还有另一个问题阻止了方法
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
从被调用。就像在一个清晰的“新鲜”项目中一样,它按预期工作。谢谢
最佳答案
这是一个可以帮助您的快速方法。
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let currentIndex = tabBar.items?.firstIndex(of: item)
if selectedIndex == 0 && currentIndex == 0 {
// Do Something
} else if selectedIndex == 1 && currentIndex == 1 {
// Do Something
}
}
关于ios - 检查用户是否在 UITabBarController 中选择了相同的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59324933/