Kunal Verma
Kunal Verma

Reputation: 602

How to dynamically populate Tab Bar controller's tab bars.? (in swift -- iOS 9)

I am developing an iOS application where the entry point is a login Screen. Which then after the login, segue to a tab bar controller. Now I want to dynamically populate the number and contents of the tab bar items based on which level the user has been logged in.

Eg ->

Level 1 Login - Tab Bar Items

Level 2 Login - Tab Bar Items

How can I dynamically bind a tab bar controller to some data, to create this kind od views.?

Upvotes: 4

Views: 3219

Answers (2)

PhilModin
PhilModin

Reputation: 21

Swift 4 - Xcode 9


You can dynamically change the array of UIViewControllers used by tabBarController at runtime by using:

self.viewControllers = arrayOfUIViewControllers

In your specific case, have 2 arrays of UIViewControllers available and when the tabBarController loads (presumably after login), present an array for level 1 or present another array for level 2 depending on the login data.


To address a more broad question, it might also be helpful to demonstrate another case where the user can generate and remove tabs dynamically at runtime.

  1. In the storyboard I have a Tab Bar Controller connected to a View Controller.
  2. View Controller has the Storyboard ID "NewTab" and is a subclass of "ViewController" it also has two UIButtons "Add" and "Remove" and the Tab Bar Item Title is set to "1"
  3. The tab names will be coming from Tabs.swift which is my placeholder for CoreData where tab data is stored.

    class Tabs: NSObject {
        let tabs: [String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    }
    
  4. ViewController.swift does pretty much everything.

    import UIKit
    class ViewController: UIViewController {
        let tabs = Tabs().tabs
        var array: [UIViewController] = []
        @IBAction func makeNewTab(_ sender: Any) {
            let newViewController: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewTab") as UIViewController
            array = (self.tabBarController?.viewControllers)!
            if array.count < tabs.count {
                newViewController.title = tabs[array.count]
                array.append(newViewController)
                self.tabBarController?.setViewControllers(array, animated: true)
            }
        }
        @IBAction func removeTab(_ sender: Any) {
            array = (self.tabBarController?.viewControllers)!
            if array.count > 1 {
                let index = array.index(of: self)
                array.remove(at: index!)
                self.tabBarController?.setViewControllers(array, animated: true)
            }
        }
    }
    

    So here you instantiate the list of tab names and an empty array of ViewControlers. @IBAction makeNewTab is linked the Add button and @IBAction removeTab is linked to the storyboard Remove button. The buttons won't work if there are less than 2 tabs or if you've run out of tab names.

I've chosen to include this example in my answer because I've been looking for it and finally put one together so maybe others will find it useful as it is related to the broad question headline.

Upvotes: 1

Oliver White
Oliver White

Reputation: 665

I would suggest following schema:

Whenever user logins with level 1 access - you need to save level1 value for this user in NSUserDefaults.

Whenever user logins with level 2 access - you need to save level2 value for this user in NSUserDefaults.

After that, when you are performing a segue to tabs controller, you need to check which value is stored in NSUserDefaults and display appropriate tabs.

Just don't forget to remove values if user log outs.

Also, I'm sure there is a better way to do this, but this is the first what came to my mind. Cheers.

Upvotes: 1

Related Questions