gpt4 book ai didi

ios - 如何在运行时在 TabBarcontroller 中安排标签栏项目?

转载 作者:行者123 更新时间:2023-11-28 06:28:17 26 4
gpt4 key购买 nike

我有带 5 个 View Controller “A”、“B”、“C”、“D”、“E”的 TabBarcontroller。但我需要在运行时决定顺序取决于 API 响应。例如。有时我需要显示“A”、“D”、“C”、“E”、“B”,即随机顺序,或者有时我只需要显示“D”、“B”、“C”, “一个”

有什么办法可以处理这种情况吗?

我正在使用 Swift 3,但即使我得到了一些逻辑或可能的方法,它也有助于解决我的问题。

我使用 Storyboard创建了 TabBar 和 viewcontroller。

enter image description here

最佳答案

if  let tabBarController = ( self.window?.rootViewController as? UITabBarController ) {
if var vcArray = tabBarController.viewControllers {
//Arrange the array according to your need and set them again.
tabBarController.viewControllers = vcArray
//Arrange the array according to your need and set them again.
var items = tabBarController.tabBar.items
tabBarController.tabBar.items = items
}
}

您必须处理 selectedViewController。我已经在 appdelegate 中编写了上面的代码,但是您可以获得 appDelegate 的对象并在上面的代码中用作 self

另请参阅 this对于另一种解决方案,您可以在其中以编程方式创建 tabCtrl 并对其进行操作。

关于ios - 如何在运行时在 TabBarcontroller 中安排标签栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41135925/

26 4 0