1

I have TabBarcontroller with 5 viewcontroller "A", "B", "C", "D", "E". But i need to decide the order at runtime depends on API response. for ex. some time I need to show "A", "D", "C", "E", "B" i.e. in random order, OR sometime i need to show only for "D", "B", "C","A"

is there any way to deal with this scenario?

I am working with Swift 3, but even if i get some logic or possible way it can be helpful to solve my issue.

I have created TabBar and viewcontroller using storyboard.

enter image description here

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
New iOS Dev
  • 1,937
  • 7
  • 33
  • 67

3 Answers3

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
    }
}

You will have to handle selectedViewController. I have written above code in appdelegate but you can get appDelegate's object and use as self in above code.

Also look at this for another solution where you can create tabCtrl programmatically and manipulate it.

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
0

You have not included code, so it is a little hard to response.

What I would do is, as soon as you have the new order, create a new TabBarController and let that new one overwrite the existing one.

To make this for the users more logical, you can move the user to the same VC "A" "B" "C" .. (in the new order) or let the user remain on the (replaced) screen (first/second etc)

Vincent
  • 4,342
  • 1
  • 38
  • 37
0

Set viewControllers property of your UITabBarController to a newly ordered viewContoller array.

Sample below is to make your viewControllers reverse ordered ( Called from 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 ] ]
    }
}
Satachito
  • 5,838
  • 36
  • 43