Sam Sam - 6 months ago 29
Swift Question

Programmatically setting tabBarItem title in Swift

I have four UIViewControllers that are linked to a UITabBarController's tab bar. I need to set the tab bar item titles outside of the storyboard, and inside of their classes.

I've tried..

class MyViewController: UIViewController {

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}
}


This is called, but the title is never set. Same with self.tabBarItem.title = "the title"

I've also tried setting the title in viewDidLoad, but that only updates the title after going to the view controller.

Thoughts?

Sam Sam
Answer

I figured it out, looks like it was being over written by awakeFromNib().

override func awakeFromNib() {
    super.awakeFromNib()

    self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}

I moved my self.title assignment there and it corrected my issue.