UINavigationController 多次連續 Push Pop 問題

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

最近要實現一個 連續 pop 兩次 又 Push 一個新的 MVC 的需求,所以把經曆寫出來分享一下。

 

NavgationController 是 iOS 中最常用的控制器了,先看官網文檔:

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but in iOS 6 and later you may subclass to customize the class behavior.

The screens presented by a navigation interface typically mimic the hierarchical organization of your data. At each level of the hierarchy, you provide an appropriate screen (managed by a custom view controller) to display the content at that level. 

 

 

大概意思就是說 導航控制器是專門用作管理你 App 視圖控制器層次的專有控制器。

 

先看一下 幾個關於導航控制器的方法:

初始化方法:

  initWithRootViewController: AViewontroller

  建立一個導航控制器,並且將AViewController 作為導航控制器的根控制器,前面說過,導航控制器只負責系統管理使用者控制器,而且是以棧的方式

管理方法:

  

– pushViewController: animated:
– popViewController: Animated:
– popToRootViewController: Animated:
– popToViewController:animated:

push ,即向棧內 push 新的 ViewController,新的 ViewController 成為了 棧頂的控制器

pop,即從棧內推出棧頂的控制器,(App 中後退 back 就是如此)。

 

比如我要在當前控制器中 push 或者 pop 一個 ViewController,一般都是這樣子調用:

  [self.navigationController 方法];

如果只是調用一次,或者 push 多次都不會有問題,但是 pop 就會有問題,因為 第一次 pop 後,當前的控制器已經出棧了,下面所有語句中的 self 都等於 nil,在 OC 中,對 nil 發送方法,肯定是無效的,所以連續 pop 的時候 需要將導航控制器儲存下來,例如 id nav = self.navigationController

 

經過測試,儲存後,可以多次pop push,另外要注意,除了最終要顯示的 ViewController 以外,其他的 pop push 操作都要設定動畫為 NO,不然會導致 navigationBar 的錯誤。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.