I have a TabBarController set as main controller tab, where names were defined using interface builder. Now I would like to change its names programmatically.
How could it be done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Updated to XCode 8
Since my original answer, a lot has happened: Swift 3, Storyboards, etc. Title is usually the one that all views share, and can be set in the attributes inspector
Also, for more control, you can always drag the
UITabBarItem, andUINavigationItemelements from the designer view. You must drag them to the view that’s gonna be displayed in the tab bar/navigation controller. Basically they store the info as “I wanna be displayed in tab bar like this”.These two elements behave exactly as accessing the view controller’s
.tabBarItemand.navigationItemproperties via code. These properties always exist in code if they are child of the corresponding object (nav has navItem, and tab has tabItem), you don’t need to add them in storyboard/xib to have them.This last thing is kinda confusing, since in the storyboard/xib it appears you’re adding them to the view controller, but in truth you’re just saying “the nib will configure these properties too”.
Original Answer
The name that appears on the tab bar comes from the
UIViewController‘stitleproperty,You can change the title at any point, and it should update the text appearing on the tab bar item. But be wary, do this as soon as possible, ideally, in the
initmethod in use (orinitWithNibName:bundle:, orinitWithCoder:).The key here, is that the init methods are called as soon as the tab bar appears on screen, as it initialises all of its view controller. If you were to do it on
viewDidLoad, that would only get called if you actually select the tab, then other family of calls, same goes forawakeFromNib,viewWillAppear:,viewDidAppear:, etc.The idea of having a title on the
UIViewController, is to keep things consistent. If you show that viewController on aUINavigationController, the navigation bar on top should use the title property, as it does when using back. TheUITabBarControlleralso respects the same title property and changes accordingly.In terms of reusability, you should be setting the title only from the inside of the
UIViewControllersubclass.The way of the Nib
Using nibs or storyboards? If you have a
UIViewController, you can give it the name straight up in the attributes inspector (or ⌥⌘4)Unfortunately, if the File Owner is the
UIViewControllersubclass, then you won’t be able to access it this way, simply because, XCode registers the File Owner as an “External Object”, and doesn’t show a configuration panel for it. 🙁Multiple titles, same view controller
But sometimes, you just want to have them named differently
Screwing with the neighbours
Each UIViewController should handle his own name, what if you want to force it from the outside (and thus, completely violating the original thing I said about reusability)?
You could also probably do it with the navigation item, but that would require more gymnastics than I’m comfortable with.