2013-05-28 47 views
5

我已將第一個視圖嵌入到NavigationController中,然後將segue設置爲push。 我想在每個視圖中更改導航欄的標題以顯示某些內容並通過我的代碼進行更新。 我試着撥打導航控制器是這樣的:想要更改NavigationItem標題

self.navigationController.navigationBar.topItem.title = @"YourTitle"; 

但這不會工作,我該怎麼辦呢?

回答

13

導航控制器獲取從視圖 - 控制標題所以 這將做的工作

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.title = @"Some Title"; 
} 
3

這個工作對我來說:

self.navigationItem.title = @"YourTitle"; 

希望它能幫助。

0

可以更換過標題視圖:

UIView* titleView = [[UIView alloc] init]; 
[self.navigationItem setTitleView: titleView]; 
0

如果u只改變導航欄標題那就試試這個 上initwithnibname方法

 [email protected]"your title"; 

,但你想給一個顏色或字體大小那麼你想採取一個有限的接口

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 

     label.backgroundColor = [UIColor clearColor]; 
     label.font = [UIFont boldSystemFontOfSize:20.0]; 
     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
     label.textAlignment = UITextAlignmentCenter; 

     label.textColor = [UIColor blackColor]; 
     self.navigationItem.titleView = label; 
     label.text [email protected]"your title"; 
     [label sizeToFit]; 
2
self.title = @"My Title" 

這將更改導航欄標題以及引用此視圖控制器的標題元素(例如,標籤欄等)

self.navigationItem.title = @"My Title"; 

這將只改變當前視圖控制器的導航欄的標題。