ios - 检测 UITabBarController 的标签栏项目更改

标签 ios delegates uitabbarcontroller uitabbar

有没有办法(例如委托(delegate))让标签栏 Controller 在您点击某个项目时执行某些操作?

该操作只能在显示 View 时执行一次,而不是在您重复按选项卡按钮时执行一次。

我正在使用这个委托(delegate),但它不能很好地工作,因为每次我触摸选项卡时它都会启动(而不是仅在每次选项卡更改时启动)

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    print("Hey")
}

最佳答案

你可以尝试在 didSelect 旁边实现 shouldSelect

func tabBarController(_ tabBarController: UITabBarController, 
              shouldSelect viewController: UIViewController) -> Bool {

    return tabBarController.viewControllers.index(of:viewController) != tabBarController.selectedIndex
}

关于ios - 检测 UITabBarController 的标签栏项目更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52636646/

相关文章:

ios - 如何以特定格式获取一年中的所有月份

ios - 如何为 ViewController segues 设置 if else 语句?

ios - 将滑动面板添加到 UITabBarController

ios - 从 tabbarcontroller 内的 View Controller 转换时加载指示器

ios - 使用 setEditing 不会使 tableview 进入编辑模式

ios - 在 iOS 中以编程方式创建渐变图像

swift - 如果快速表格 View 中有更多(大约 10 到 20)个部分,如何返回部分值?

iphone - didSelectRowAtIndexPath 不工作

ios - Swift 中可能存在类中的第二个委托(delegate)吗?

ios - 在 AppDelegate 中引用从 StoryBoard 创建的 TabbarController?