Hussam Dabaan
Hussam Dabaan

Reputation: 27

How to put viewControllers into loop array?

I am trying to use customized tab viewController, in which the user can add/re-arrange them the way he/she likes. in the following code I only need to return the array count and display the array items, I managed to do it by a number of the array, but when using it as a single array code, it displays only one view controller!!

here is the code:

class ContainerController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let selectedViewControllers = [Home(),Index(),Favourite(),Library()]
        let selectedViewControllersProperties = ["Home","Index","Favourite","Library"]

        viewControllers = [createController(title: "\(selectedViewControllersProperties[0])".localized, imageName: "\(selectedViewControllersProperties[0])", vc: selectedViewControllers[0]),
        createController(title: "\(selectedViewControllersProperties[1])", imageName: "\(selectedViewControllersProperties[1])", vc: selectedViewControllers[1]),
        createController(title: "\(selectedViewControllersProperties[2])", imageName: "\(selectedViewControllersProperties[2])", vc: selectedViewControllers[2]),
        createController(title: "\(selectedViewControllersProperties[3])", imageName: "\(selectedViewControllersProperties[3])", vc: selectedViewControllers[3]),]
    }

    // MARK: - Handlers
    func createController(title: String, imageName: String, vc: UIViewController) -> UINavigationController{

        let recentVC = UINavigationController(rootViewController: vc)

        recentVC.tabBarItem.title = title
        recentVC.tabBarItem.image = UIImage(named: imageName)
        return recentVC
    }
}

When trying to use this array instead, it shows only ONE viewController:

let tabCount = selectedViewControllersProperties.count - 1

for i in 0...tabCount {
      viewControllers = [createController(title: "\(selectedViewControllersProperties[i])".localized, imageName: "\(selectedViewControllersProperties[i])", vc: selectedViewControllers[i])]
}

What I did wrong here, please help me to resolve it.

Upvotes: 0

Views: 211

Answers (1)

Sweeper
Sweeper

Reputation: 272750

In each iteration of the for loop, you are just setting a new array (with only one item) to viewControllers, which is why viewControllers will only have one item in the end as well.

You need to append the new VCs to the array, instead of using =:

let tabCount = selectedViewControllersProperties.count

// ..< is safer
var viewControllers = [] // you don't seem to have declared this beforehand.
for i in 0..<tabCount {
     viewControllers.append(createController(title: "\(selectedViewControllersProperties[i])".localized, imageName: "\(selectedViewControllersProperties[i])", vc: selectedViewControllers[i]))
}

You can also do this with map:

viewControllers = (0..<tabCount).map {
    createController(title: "\(selectedViewControllersProperties[$0])".localized, imageName: "\(selectedViewControllersProperties[$0])", vc: selectedViewControllers[$0])
}

Upvotes: 1

Related Questions