iOS中設置backBarButtonItem的title和action


一、 設置title

在需要顯示該返回鍵的前一個Controller中設置

   1:   navigationItem.backBarButtonItem = UIBarButtonItem(title: "注銷", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

二、添加返回時執行Action:

你可以嘗試一下在上一行代碼中的target和action處填入事件監聽的處理方法,但是不會起作用的。

backBarButtonItem的描述

Discussion

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

說明了backBarButtonItem只能自定義image和title,不能重寫target or action,系統會忽略其他的相關設置項。

如果硬是需要重寫action做一些其他的工作,則需要自定義一個leftBarButtonItem,因為系統定義leftBarButtonItem的顯示優先級比backBarButtonItem優先級高,當存在leftBarButtonItem時,自動忽略backBarButtonItem,達到重寫backBarButtonItem的目的。

 

 

注意點

一般我們取導航控制器上的Item這樣取:

navigationItem.backbarItem

如果這樣表示套了兩層的導航欄的最外面那層的Item,一般情況下沒有這樣的嵌套。

navigationController.navigationItem.backBarButtonItem

 

 

Ref:

http://blog.csdn.net/dreamno23/article/details/21085783

http://www.cnblogs.com/ihojin/p/change-backbarbuttonitem-text.html


免責聲明!

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



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