presentModalViewController和dismissModalViewControl

在實際開發中,若是要彈出視圖:
咱們經常使用到 presentModalViewController方法和 dismissModalViewControllerAnimated方法。
presentModalViewController:彈出視圖
dismissModalViewControllerAnimated隱藏 視圖

貼代碼:

彈出視圖:

FeedbackViewController *feedbackViewController = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil]; app

    UINavigationController *navigationController = [[UINavigationControllerallocinitWithRootViewController:feedbackViewController]; 測試

    [self presentModalViewController:navigationController animated:YES]; this


隱藏視圖:

[self dismissModalViewControllerAnimated:YES]; spa


關於這兩個方法的幾點說明:

1.iPhone上彈出/隱藏 視圖時,使用爲全屏模式

On iPhone and iPod touch devices, the view of modalViewController is always presented full screen.

2.搞清楚誰是presenting,誰是presented

若是A彈出B,那麼A爲 presenting,B爲 presented

3.隱藏視圖的策略

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, however, it automatically forwards the message to the presenting view controller.

咱們假如A彈出B
就是說, A負責隱藏B;若是咱們在B中調用 dismissModalViewControllerAnimated方法,那麼編譯器,自動將消息發送給A。
等等, 什麼消息?
簡單的理解,當執行presentModalViewController:方法:在A彈出B時:

執行A的 viewWillDisappear方法,
通知B執行本身的 viewWillAppear方法和 viewDidAppear方法
執行A的 viewDidDisappear 方法

當執行dismissModalViewControllerAnimated方法:隱藏B時:

執行B的 viewWillDisappear
通知A執行本身的 viewWillAppear方法和 viewDidAppear方法
執行B的 viewDidDisappear方法

如下我作了個測試來輸出一輪AB切換:
A:More
B:Feed

2012-12-27 14:01:23.666 WTV[1627:11303] -More--viewWillDisappear---- orm

2012-12-27 14:01:23.672 WTV[1627:11303] -Feed--viewWillAppear---- 對象

2012-12-27 14:01:24.086 WTV[1627:11303] -Feed--viewDidAppear---- 接口

2012-12-27 14:01:24.087 WTV[1627:11303] -More--viewDidDisappear---- ci

2012-12-27 14:01:25.745 WTV[1627:11303] -Feed--viewWillDisappear---- 開發

2012-12-27 14:01:25.745 WTV[1627:11303] -More--viewWillAppear---- 編譯器

2012-12-27 14:01:26.156 WTV[1627:11303] -More--viewDidAppear----

2012-12-27 14:01:26.157 WTV[1627:11303] -Feed--viewDidDisappear----


當咱們信心慢慢,慶幸咱們能夠了解了這兩個方法時,悲劇發生了:

4.蘋果官方已經把 這兩個方法  Deprecated in iOS 6.0. 

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated;


- (void)dismissModalViewControllerAnimated:(BOOL)animated;


取而代之的是:

[self presentViewController:navigationController

                       animated:YES

                     completion:^(void){

                         // Code

                     

                     }];


[self dismissViewControllerAnimated:YES

                             completion:^(void){

                                 // Code

                             }];


新接口的差異是提供了一個參數,容許你傳入一個block。這個block的回調方法在VC的viewWillDisappear方法後調用。也就是被隱藏的VC對象被釋放後運行回調。
這樣作的好處:能夠方便作多個UI效果之間的銜接和轉換。

用新的吧!與時俱進!

但願對你有所幫助!
相關文章
相關標籤/搜索