2016-12-14 44 views
1

我有TabBarcontroller 5视图控制器“A”,“B”,“C”,“D”,“E”。但我需要决定运行时的顺序取决于API响应。前者为 。有时我需要以随机顺序显示“A”,“D”,“C”,“E”,“B”,或者某时我只需要显示“D”,“B”,“C”, “A”如何在运行时在TabBarcontroller中安排tabbar项目?

有没有办法处理这种情况?

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

我创建了使用故事板的TabBar和viewcontroller。

enter image description here

+0

如何创建标签栏控制器及其视图控制器? – rmaddy

+0

这个问题很好,但不应该这样做,因为人们有肌肉记忆,你的功能会搞砸了。 –

+0

@rmaddy我已经更新了我的问题 – sss

回答

1
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并对其进行操作。

0

您还没有包含的代码,因此它是一个有点难以应对。

我会做的是,只要你有新的订单,创建一个新的TabBarController,让新的覆盖现有的。

为了让用户更符合逻辑,可以将用户移动到相同的VC“A”“B”“C”(按照新顺序),或让用户留在(替换)屏幕上(第一/第二等)

+0

这是可能的,如果在故事板中创建tabbarcontroller? – sss

0

将UITabBarController的viewControllers属性设置为新订购的viewContoller数组。

下面的示例是使您的viewControllers反向排序(从AppDelegate调用)。

if let wTB = (self.window?.rootViewController as? UITabBarController) { 
    if let wVCs = wTB.viewControllers { 
     wTB.viewControllers = [ wVCs[ 4 ], wVCs[ 3 ], wVCs[ 2 ], wVCs[ 1 ], wVCs[ 0 ] ] 
    } 
} 
相关问题