【发布时间】:2015-04-04 11:35:25
【问题描述】:
简短的解释。
我有一个ContainerViewController,我将其推送到navigationStack。
ContainerViewController 有 2 个孩子 UIViewController。一个SlidePanelViewController(滑出菜单)和一个CenterViewController(内容),我的CenterViewController是一个UITabBarController,目前有2个UIViewController。
我的菜单中有一个按钮,我需要更改我的tabBarController 中的UIViewController。但是,当我调用我的函数时,什么也没有发生。
这是我要调用的函数:
func premiumFormulaTapGesture() {
tabBarController?.selectedIndex = 1
}
(我也尝试将其设置为 0 和 2。仍然没有结果。)我尝试将 self. 放在它前面,但没有任何运气。
我还尝试将它作为一个函数放在我的ContainerViewController 中。但这似乎也不起作用。
这是我设置UITabBarController的方法:
var centerTabBarController: UITabBarController!
在ViewDidLoad():
let tabBarController = UITabBarController()
let suggestionsVC = mySuggestions_VC()
let testVC = detaiLSuggestion_VC()
let controllers = [suggestionsVC,testVC]
tabBarController.setViewControllers(controllers, animated: false)
centerViewController = tabBarController
view.addSubview(tabBarController.view)
addChildViewController(tabBarController)
我的tabBarController 确实出现了。而且我可以手动点击上面的 2 个按钮,它可以在 viewControllers 之间切换,如预期的那样。我稍后计划隐藏UITabBarController,并使用菜单。 UITabBarController 将成为我从菜单中更改 UIViewController 的方法。
也只是为了澄清。我没有使用故事板。
任何帮助更改我的tabBarController 中的viewControllers 将不胜感激!
【问题讨论】:
-
当您致电
tabBarController?.selectedIndex = 1时,tabBarController是什么? (我看到您在viewDidLoad中本地定义了该变量名,但在premiumFormulaTapGesture中指的是什么?) -
你完全正确。我创建了一个名为 menuTabBarController 的新 tabBarController,在类上方定义了它。并将我的功能更改为 menuTabBarController.selectedIndex = 1 并且效果很好。非常感谢。如果您想继续添加答案,我会接受:)
标签: ios swift uiviewcontroller uitabbarcontroller uitabbaritem