【问题标题】:changing tabBarController Index from another viewController programatically以编程方式从另一个 viewController 更改 tabBarController 索引
【发布时间】: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


【解决方案1】:

Phillip Mills 尚未发布答案。所以我只是关闭未解决的问题。

我的问题是,在将名称更改为“menuTabBarController”后,有 2 个 UITabBarControllers 命名为相同的“tabBarController”,一切正常。

再次感谢 Phillip Mills 解决了我在 cmets 中的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多