2011-08-30 44 views
19

我目前正在嘗試更改已加載tablecell觸摸的子視圖的後退按鈕文本。但即使我試圖實現它的方式,它仍然顯示後退按鈕中的父母標題。如何更改uinavigationbar後退按鈕文本

我想但是它不工作到一個新的數值裝入後退按鈕viewDidLoad方法裏,像這樣

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init]; 
myBarButtonItem.title = @"Back"; 
self.navigationItem.backBarButtonItem = myBarButtonItem; 
[myBarButtonItem release]; 

+0

這是在主線程上執行嗎? –

+0

老實說,我不確定,我不確定有關ios中的線程。如果這有助於回答您的問題,我不會創建線程以我的知識。 –

+1

重複的問題。看到這個答案:http://stackoverflow.com/a/4291832/1627732 – Yoga

回答

0

你的代碼目前看起來很好。

此代碼的

[super viewDidLoad]; 

聲明(錯誤的)前執行或之後(好)?

+0

我之後正在執行它。 –

1

這不會像你想要做的那樣工作。導航按鈕將始終具有前一個視圖的標題。你可以做什麼 - 在推新視圖之前改變第一視圖的標題。這是我能找到的唯一解決相同問題的方法。

+1

然後當你回去時你必須改回你的頭銜..這是一個可怕的解決方案,如果它的唯一可能性..手指越過另一種方式。 –

9

好吧,算了一下,我發佈了這個代碼在父視圖tableView:didSelectRowAtIndexPath:方法。這是我用一個用戶可以選擇多個表格的外觀。希望這可以幫助。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    if (indexPath.section == 0) { //--- picks (first) section 

    ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:childViewController animated:YES]; 
    //--- this sets the back button to "Back" every time you load the child view. 
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease]; 

     if(indexPath.row == 0) { //--- picks row 
      childViewController.title = @"Bike"; 
     } 
     if(indexPath.row == 1) { 
      childViewController.title = @"Model"; 
     } 
     [vehicleSearchResponseTableViewController release]; 
    } 


} 
+0

如果您在堆棧中有多個視圖控制器,這是唯一真正有效的解決方案。謝謝。 – Winston

-1

This article應該做你想做的。

從筆者:

正如你可以在上面看到,所有你需要做的是設置控制器的leftBarButtonItem,它會隱藏後退按鈕。選擇器handleBack現在處理後退事件,然後需要手動將視圖控制器從UINavigationController的堆棧中彈出。你可以在那裏實現你自己的代碼,甚至阻止離開頁面。

30

你需要改變self.navigationItem.backBarButtonItem從以前的看法,而不是當前的看法(我知道,這似乎有點不合邏輯)。例如,在你的表視圖,你可以做到以下幾點:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setTitle:@"My title"]; 
    UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:@"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:@selector(mySelector:)]; 
    self.navigationItem.backBarButtonItem = boton; 
    [boton release]; 
} 
+0

如果他是用西班牙文寫的話,則不需要;) – Steven07

24

在這裏,直到您已經閱讀並重新閱讀和玩各設置的文檔不是那麼清楚。
要更改默認的標題後退按鈕如果您希望按鈕是不可見添加此行viewDidLoad()

self.navigationController.navigationBar.topItem.title = @"Your Label"; 

- 然後將值設置爲@""空字符串。

+2

這是一種黑客攻擊,但是如果您在回去之前重新設置前一個筆尖的標題,它會起作用 –

+0

Downvoted。考慮到其他選擇,這不應該被認爲是一個可行的解決方案。如果我們由於缺乏對後退按鈕行爲的控制而面臨這種情況,那麼這將是一個可接受的黑客行爲,但仍然是一種黑客行爲。 –

+0

TopItem並不總是一個BackButton在我的情況下TopItem是查看標題 –

-3

一個好辦法做到這一點是調用子這樣之前設置在父視圖控制器返回按鈕的標題:

[self.navigationItem.backBarButtonItem setTitle:@"Your Custom Title"]; 

此標題將被放置在後退按鈕上的孩子視圖。重要的一點是,這個標題在子視圖中用作父視圖的後退按鈕。

+0

根據你的解釋,你不是指[self.navigationItem setTitle:@「...」],甚至只是'self.title = @「...」'? –

+1

默認情況下,self.navigationItem.backBarButtonItem爲零,因此您無法使用此更改後退按鈕儀式。 – Peter

2

使用Xcode 5更改後退按鈕名稱的最佳方法是編輯後退按鈕將返回到的View Controller上的導航項目的IB中的後退按鈕字段。例如,如果您有一個列表TableViewController進入詳細信息屏幕,請更改列表TableViewController的導航項(IB中)的後退按鈕以更改詳細信息屏幕顯示的後退按鈕名稱。

+0

這是你想要做的方式。謝謝彼得。 – JVillella

0

viewDidLoad中

self.navigationController!之後.navigationBar.backItem?.title僞= 「後退」

0

我的建議是添加一個單獨的標籤,以家長頁的標題欄。 對我來說工作很好。

let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25), height:30)) 
     titleLabel.text = "Title" 
     titleLabel.textAlignment = .center 
     titleLabel.textColor = UIColor.white 
     titleLabel.font = UIFont.systemFont(ofSize: 20) 
     navigationItem.titleView = titleLabel 
相關問題