Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController Title attribute in Storyboard

I am setting the title field of a UIViewController via Interface Builder/Storyboard: enter image description here

This view controller is nested in a UINavigationController which in turn is nested within a UITabBarController. When I run the app, I my navigation item has no title, neither does the tab bar item.

If I explicitly set the view controller's navigation item's title, and also it's tab bar item's title in interface builder, then it works just fine.

I am wondering:

a)If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behaviour?

b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.

Thanks!

like image 540
0xSina Avatar asked Sep 17 '13 08:09

0xSina


People also ask

What is UIViewController in IOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


2 Answers

You can set the title of the UINavigationBar in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar.

Setting the title in code offers some different possibilities.

self.title = @"Your title"; will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.

self.navigationItem.title = @"Your title"; will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.

self.navigationController.title = @"Your title"; will set the title of a UITabBarItem but NOT the UINavigationBar.

like image 120
hgwhittle Avatar answered Sep 21 '22 19:09

hgwhittle


Step 1

If you're looking at a Xib in Xcode's Interface Builder, take a look in the "Document Outline" panel (second panel from the left). Expand the view controller you're working with until you find an icon labelled: Navigation Item.

Document Outline panel showing Navigation Item

Step 2

If you then highlight the Navigation Item and open up the Utilities panel (the farthest on the right), and click the Attributes Inspector, you'll see where you can set the title of the view controller. This is the way to do it in Interface Builder, rather than doing it through code.

Utilities panel showing how to set title of view controller

like image 33
Mario Avatar answered Sep 19 '22 19:09

Mario