简体   繁体   English

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

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

I have a tab bar controller with 3 tab bar buttons. 我有一个带有3个标签栏按钮的标签栏控制器。 At the moment it looks like this: 现在看起来像这样:

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]

}
}

Right now, with the code above, all the view controllers are sitting inside the CustomTabBarController 现在,使用上面的代码,所有视图控制器都位于CustomTabBarController内部

I want the the middle tab bar button, the secondTabBarNavigationController to present a view controller, specifically the UIImagePickerController for a user to select an image, similar to Instagram. 我希望中间的标签栏按钮, secondTabBarNavigationController呈现一个视图控制器,特别是UIImagePickerController供用户选择图像,类似于Instagram。

How can this be achieved? 如何做到这一点? I am not using storyboards 我没有使用情节提要

Just implement the func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool delegate method of the tabBar controller. 只需实现func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool tabBar控制器的func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool委托方法。

When the desired tab is pressed, present a controller and return false , otherwise return true . 当按下所需的选项卡时,显示一个控制器并返回false ,否则返回true

I would also associate an empty UIViewController instance to the tab. 我还将一个空的UIViewController实例关联到该选项卡。

Ex. 防爆。

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.

相关问题 如何在标签栏控制器视图内的按钮按下时在标签栏下显示另一个视图控制器? - How to present another view controller under tab bar on button press inside a tab bar controller view? 如何从UI视图控制器显示选项卡栏控制器 - How to present a Tab Bar Controller from a UI View Controller 从标签栏控制器以模态方式呈现视图 - Present a View modally from a tab bar controller 从选项卡栏控制器以模态方式呈现视图控制器 - Present a View Controller modally from a tab bar controller 来自UITabBarController的当前视图控制器,未显示选项卡栏 - Present view controller from UITabBarController without tab bar showing 如何在标签栏控制器上显示模态视图? - How to present a modal view on top of tab bar controller? 如何在选项卡栏项目上轻按以方式显示View Controller - How to modally present a View Controller on tap of tab bar item 在选项卡栏控制器之前显示视图 - Present a View before Tab Bar Controller 单击第一个视图控制器中的按钮时如何通过选项卡栏控制器从一个视图控制器转到另一个视图控制器 - How to go from one view controller to another view controller through tab bar controller when Click a button in the first view controller 从标签栏按钮将视图控制器推到导航控制器上 - Pushing a view controller onto a navigation controller from a tab bar button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM