【问题标题】:How to present a view controller from tab bar button如何从标签栏按钮呈现视图控制器
【发布时间】:2016-12-22 09:47:39
【问题描述】:

我有一个带有 3 个标签栏按钮的标签栏控制器。目前它看起来像这样:

class CustomTabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    let firstTabBarController = FirstController()
    let firstTabBarNavigationController = UINavigationController(rootViewController: firstTabBarController)
    firstTabBarNavigationController.tabBarItem.title = "First Tab"

    let secondTabBarController = SecondController()
    let secondTabBarNavigationController = UINavigationController(rootViewController: secondTabBarController)
    secondTabBarController.tabBarItem.title = "Second Tab"

    let thirdTabBarController = ThirdController()
    let thirdTabBarNavigationController = UINavigationController(rootViewController: thirdTabBarController)
    thirdTabBarNavigationController.tabBarItem.title = "Third Tab"

    viewControllers = [firstTabBarNavigationController, secondTabBarNavigationController, thirdTabBarNavigationController]

}
}

现在,使用上面的代码,所有视图控制器都位于 CustomTabBarController

我希望中间标签栏按钮 secondTabBarNavigationController 呈现一个视图控制器,特别是 UIImagePickerController 供用户选择图像,类似于 Instagram。

如何做到这一点?我没有使用故事板

【问题讨论】:

  • 为什么要设置 tabBarController.delegate 属性而不是 self.delegate ?您正在做的事情是错误的,因为您正在尝试设置可选super tabBarController 的委托。编辑它,它的工作原理
  • 啊,我明白了,我不知道!谢谢卢卡,工作得很好。

标签: ios swift


【解决方案1】:

只需实现tabBar控制器的func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool委托方法即可。

按下所需的选项卡时,显示一个控制器并返回false,否则返回true

我还将一个空的UIViewController 实例关联到选项卡。

例如

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if (tab for this controller is equal to the expected tab) {
        // present you controller
        return false
    } else {
        return true
    }
}

【讨论】:

  • 如何检查哪个选项卡被按下?
  • 那是你@luke。您应该能够检查特定 UIViewController 实例的相应选项卡是什么。至少是UITabBarController 模式所期望的。
  • 我在这里有点困惑。我不知道if tabFor(viewController) == .cameraTab 是什么意思。
  • @luke 抱歉,这只是一个占位符,让您了解应该在那里实现什么,我将用评论替换
  • 不确定我是否遗漏了什么 - 当我点击一个标签时,func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 似乎从来没有被调用过。当我输入shouldSelect 时,自动完成功能也不允许我选择功能。
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多