iOS中設定backBarButtonItem的title和action

來源:互聯網
上載者:User

標籤:

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

iOS中設定backBarButtonItem的title和action

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.