標籤: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 的錯誤。