IOS 改變Navigation的返回按鈕


兩個辦法:

1, 手動為每一個UIViewController添加navigationItem的leftButton的設置代碼

2,為UINavigationController實現delegate,在pop和push的時候改變當前和上一頁的navigationItem.title

以下是封裝的一些基礎方法,供參考:

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setTitle:(NSString*)title;

+ (void) navigationItem:(UINavigationItem*)navigationItem

          setBackButton:(NSString*)title

              addTarget:(id)target

                 action:(SEL)action;

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setImage:(NSString*)imageString;

無奈之下,只好研讀UINavigationController Class Reference去,在“Updating the Navigation Bar”小節,有這么一段話:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

  • If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

  • If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItemproperty, the navigation bar displays that item.

  • If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

我大致解釋一下,使用pushViewController切換到下一個視圖時,navigation controller按照以下3條順序更改導航欄的左側按鈕。

1、如果B視圖有一個自定義的左側按鈕(leftBarButtonItem),則會顯示這個自定義按鈕;

2、如果B沒有自定義按鈕,但是A視圖的backBarButtonItem屬性有自定義項,則顯示這個自定義項;

3、如果前2條都沒有,則默認顯示一個后退按鈕,后退按鈕的標題是A視圖的標題。

按照這個解釋,我把UIBarButtonItem *backItem……這段代碼放在A視圖的pushViewController語句之前。

OK問題解決了,B視圖的后退按鈕的標題變成back了。

eg:

UIBarButtonItem*backItem=[[UIBarButtonItemalloc]init];

    backItem.title=@"戻る";

    self.navigationItem.backBarButtonItem=backItem;

    [backItem release]

==============

新方案:

self.navigationController.navigationBar.topItem.title=self.message;

    self.navigationController.navigationBar.tintColor=[UIColorblackColor];

    UIBarButtonItem*backButton = [[UIBarButtonItemalloc] initWithTitle:@" fan hui "style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(PopViewController)];

    self.navigationItem.leftBarButtonItem= backButton;

以上轉自:http://hi.baidu.com/myyuchong/item/1e6db306c5d34de1359902a1

PS:在第三種情況的時候,自動創建的返回按鈕只取了A視圖的title值,其他屬性並沒有復制,看來要改變顏色等別的屬性還得自定義一個返回按鈕啊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM