Himabindu
Himabindu

Reputation: 85

Swift iOS Dynamic Tab bar from json response

I'm new to iOS/Swift. My application is using a json data and I have to create tab bar using the json response. I mean, I get Array of Title from json and I have to create tab bar items based on that array. The Array data/count may change and the app should display the tab bar accordingly. I am trying to create the tab bar programatically without storyboards (as this is huge tab bar)

So far, I have tried the below code -

func tabBarCustom() {

    let tt = UITabBarController()

    var array1 = [UIViewController]()
    var controller1 = UIViewController()


    for i in 0..<navgTitle.count {

        controller1 = UIViewController(nibName: "WeatherViewController", bundle: nil)

        controller1.title = navgTitle[i]

    controller1.tabBarItem = UITabBarItem(title: navgTitle[i], image: .none, tag: 1)

        array1.append(controller1)



    }

    print(array1)

    tt.viewControllers = array1

    self.view.addSubview(tt.view)

}

The above code is failing saying - Could not load NIB in bundle: 'NSBundle' with name 'WeatherViewController' I am not sure how to create multiple view controllers automatically using the title array, taking title as name of the view controller. is this possible? and how to loop in the array to create view controllers for each tab bar item

please help.Thank you

Upvotes: 2

Views: 578

Answers (1)

Caroline Harrison
Caroline Harrison

Reputation: 178

Are you using nibs? If so, your project can't find the nib file, and you should check out this: Could not load NIB in bundle: 'NSBundle'. Or are you using Storyboard? If you are trying to initialize from the storyboard, you'll do something like this:

let vc = sb.instantiateViewController(withIdentifier: "WeatherViewController")

(just be sure to add WeatherViewController as the view controller's identifier).

OR are you using code? If you are trying to create your view controller from code, you simply need to do WeatherViewController().

As for the tab bar, you're in the right direction it's just that creating the view controller is failing.

Upvotes: 1

Related Questions