iOS連續dismiss幾個ViewController的方法

來源:互聯網
上載者:User

標籤:ted   需求   特殊   ica   get   bsp   連續   地方   需要   

原文連結:http://blog.csdn.net/longshihua/article/details/51282388

presentViewController是經常會用到的展現ViewController的方式,而顯示和去除presentViewController也是很簡單的,主要是下面兩個方法:

 

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completionNS_AVAILABLE_IOS(5_0);

 

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^__nullable)(void))completionNS_AVAILABLE_IOS(5_0);

 

但是有的時候我們的需求很特殊,比如在一個presentViewController裡要present另一個viewController,甚至再present一個viewController,然後可能在某個時候APP發出一條訊息,需要一下子dismiss掉所有的viewController,回到最開始的視圖控制器,這時候該如何辦呢?下面一起來看看解決辦法?

 

首先,必須知道現在整個APP最頂層的ViewController是哪個,我的做法是建立一個父視圖控制器,稱為BaseViewController,然後在該視圖控制器的viewWillAppear進行記錄操作,使用視圖控制器的presentingViewController屬性記錄當前視圖控制器,然後對於需要進行present操作的視圖控制器,繼承於BaseViewController,那麼每次present一個新的視圖控制器,父視圖控制器的viewWillAppear方法都會被執行:

 

-(void)viewWillAppear:(BOOL)animated{        AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];      delegate.presentingController = self;    }  

 


最後,在需要處理時間的地方(如:點擊事件),在點擊事件的方法中加入如下代碼,即可回到最初視圖控制器顯示頁面:

 

- (void)clickButton:(id)sender {            AppDelegate *delegate=(AppDelegate *)[[UIApplicationsharedApplication]delegate];          if (delegate.presentingController)      {          UIViewController *vc =self.presentingViewController;            if ( !vc.presentingViewController )   return;                    //迴圈擷取present出來本視圖控制器的視圖控制器(簡單一點理解就是上級視圖控制器),一直到最底層,然後在dismiss,那麼就ok了!          while (vc.presentingViewController)          {              vc = vc.presentingViewController;          }                    [vc dismissViewControllerAnimated:YEScompletion:^{                        }];      }  } 

 


 

屬性解析:

 

presentedViewController:The view controller that is presented by this view controlller(read-only),被本視圖控制器present出來的的視圖控制器(唯讀)

presentingViewController:The view controller that presented this view controller. (read-only),present出來本視圖控制器的視圖控制器(唯讀)

iOS連續dismiss幾個ViewController的方法

相關文章

聯繫我們

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