一、 設置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
