繁体   English   中英

如何通过标签栏按钮显示视图控制器

[英]How to present a view controller from tab bar button

我有一个带有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。

如何做到这一点? 我没有使用情节提要

只需实现func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM