ios - 检查用户是否在 UITabBarController 中选择了相同的选项卡

标签 ios swift uitabbarcontroller

我有一个 UITabBarController 的子类。问题是:有没有办法检查用户是否选择了已经选择的 UITabBarItem

我试过了 didSet 用于 selectedIndexselectedViewController 以及不同的委托(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/

相关文章:

swift - Xcode [Swift] 中的线程 1 信号 SIGABRT 错误

ios - 错误-段错误 : 11 in Xcode 10

ios - 更改 uitabbar 项目图像的按钮

ios - 将程序从 XCode 传输到 iPad

iPhone - 如何引用容器中的文本框

ios - 所有未在 UITableView 中呈现的解析对象

ios - 解包可选值时发现 nil

ios - 确定在 tabbarcontroller 中选择哪个选项卡

ios - 画一个圆圈,用不同的颜色填充不同的部分

iphone - Objective-C 中关于 "property"的一些困惑