从 UITabBarController 初始化 viewController [英] Initialize viewControllers from a UITabBarController
问题描述
我有一个 UITabBarController.使用界面生成器,我添加了多个视图控制器.这些 viewController 都是同一个类,但唯一改变的是参数.
I have a UITabBarController. Using interface builder I'm adding multiple view controllers. These viewControllers are all the same class but the only thing that changes is a paramenter.
我想扩展 UITabBarController 以便在视图控制器初始化时添加此参数,但我不知道如何在 UITabBarController 扩展中执行此操作.在哪里做这件事的正确地方???
I want to extend the UITabBarController so I can add this parameter when the view controllers are initialized but I have no clue how to do this in the UITabBarController extension. Where is the right place to do this???
推荐答案
我也遇到过这样的问题,我想在选项卡栏控制器的第五个选项卡中显示弹出视图控制器,我用过这个:
i also faced issue like that, in which i want to show pop over view controller in fifth tab of tab bar controller, i used this:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if(viewController == tabBarController.viewControllers?[4]){
let storyboard : UIStoryboard = UIStoryboard(name: "AfterLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MorePopUpVC")
vc.modalPresentationStyle = .popover
let popover = vc.popoverPresentationController
popover?.delegate = self
popover?.permittedArrowDirections = .down
popover?.sourceView = self.tabBar
popover?.sourceRect = ((self.orderedTabBarItemViews()).last?.frame)!
vc.preferredContentSize = CGSize(width: 120, height: 132)
present(vc, animated: true, completion:nil)
return false
}
return true
}
这是我如何更改第五个标签的操作,
this is how i change action of fifth tab,
你可以像这样使用它:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
let storyboard : UIStoryboard = UIStoryboard(name: "YOUR STORYBOARD NAME", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "YOUR VIEW CONTROLLER IDENTIFIER")
if(viewController == tabBarController.viewControllers?[4]){
vc.type = "PARAMETER YOU WANT"
}
present(vc, animated: true, completion:nil)
return false
}
我认为它有帮助.
这篇关于从 UITabBarController 初始化 viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!