[英]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.