簡體   English   中英

如何在另一個ViewController中關閉一個ViewController?

[英]How to dismiss a viewcontroller within another ViewController?

假設我有一個名為ABCDE的視圖控制器,我以presentModel的方式ViewConrollers打開這些ViewConrollers 當我在ViewController E I要打開另一個ViewController在presentModel方式F。 在那個F ViewController我有一個后退按鈕。 當我單擊它時,應關閉F並顯示A ViewController 但是現在解散時顯示E。 單擊F的后退按鈕時,如何關閉除A以外的所有其他視圖控制器

請幫我。 謝謝

更新

-(IBAction)dismisthis:(id)sender{


UIViewController *dismissingViewController = self.presentingViewController;
while (dismissingViewController.presentingViewController != nil && [dismissingViewController isKindOfClass:[FrontViewController class]]) {
    dismissingViewController = self.presentingViewController;
}




[dismissingViewController dismissViewControllerAnimated:NO completion:NULL];

如果您關閉了顯示另一個視圖控制器的視圖控制器,則整個層次結構都將被關閉。 有多少個級別都沒關系。 因此,您要做的就是找到您的視圖控制器A並告訴它解散其顯示的視圖控制器。

如果A始終是堆的底部,則可以使用簡單的循環找到它:

UIViewController *dismissingViewController = self.presentingViewController;
while (dismissingViewController.presentingViewController != nil) {
    dismissingViewController = self.presentingViewController;
}

[dismissingViewController dismissViewControllerAnimated:YES
                                             completion:NULL];

目前,我可以想到三種解決方案

  1. 您應該在Appdelegate的stackObject(是一個數組)中跟蹤所有viewControllers。 當您希望它訪問時,獲取這些數組並關閉所有視圖控制器objetcs。

  2. 您可以觀察的每個ViewController都將監聽NSNotification,以監聽通知。 當您在“ F”中需要它時,只需發布​​通知,此通知將關閉eviewcontrollers

  3. 轉到NavigationController,以便您可以推送到rootviewcontroller

使用F和A的NSNotificationCenter發送Notification方法將如何處理。 一旦A收到通知,它將調用dismissViewController ,我認為它將全部關閉。

嘗試這個:

UIViewController *rootVC = [UIApplication sharedApplication].delegate.window.rootViewController;
[rootVC dismissViewControllerAnimated:YES completion:nil];

// One-liner
// [[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

將A設為rootViewController並在“ F”的“后退”按鈕上使用以下代碼:

[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissViewControllerAnimated:true completion:nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM