0

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.

1 Answer 1

1

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])
}
Sign up to request clarification or add additional context in comments.

4 Comments

But the first append didn't
@HussamDabaan You probably declared the array but haven't show it then. Delete the word var and it should work.
Error: Value of type '[UIViewController]?' has no member 'append'
@HussamDabaan Add a question mark ? before the .append.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.