user5216681
user5216681

Reputation: 11

Cant add text to listview in Tab bars Swift2

I use this code to set titles to tabbar items. It works great with the first 5. But when the sixth should add text to the listview under "more" the app crash. How to set the text on that list?

    let tabArray = self.tabBarController?.tabBar.items as NSArray!

    let tabItem1 = tabArray.objectAtIndex(0) as! UITabBarItem
    let tabItem2 = tabArray.objectAtIndex(1) as! UITabBarItem
    let tabItem3 = tabArray.objectAtIndex(2) as! UITabBarItem
    let tabItem4 = tabArray.objectAtIndex(3) as! UITabBarItem
    let tabItem5 = tabArray.objectAtIndex(4) as! UITabBarItem
    let tabItem6 = tabArray.objectAtIndex(5) as! UITabBarItem

    tabItem1.title = "Home"
    tabItem2.title = NewArray().12
    tabItem3.title = NewArray().13
    tabItem4.title = NewArray().14
    tabItem5.title = NewArray().15
    tabItem6.title = NewArray().16

Upvotes: 1

Views: 103

Answers (1)

Bannings
Bannings

Reputation: 10479

Try this:

let viewControllerArray = self.tabBarController?.viewControllers as [UIViewController]!
let tabItem1 = viewControllerArray[0].tabBarItem
let tabItem2 = viewControllerArray[1].tabBarItem
let tabItem3 = viewControllerArray[2].tabBarItem
let tabItem4 = viewControllerArray[3].tabBarItem
let tabItem5 = viewControllerArray[4].tabBarItem
let tabItem6 = viewControllerArray[5].tabBarItem

tabItem1.title = "Home"
tabItem2.title = NewArray().12
tabItem3.title = NewArray().13
tabItem4.title = NewArray().14
tabItem5.title = NewArray().15
tabItem6.title = NewArray().16

Upvotes: 1

Related Questions