2016-03-08 70 views
0

裝載2的UIViewController當用戶點擊一個按鈕,我顯示UIViewController如下:辭退這是通過presentViewController

NSString * storyboardName = @"Main"; 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; 
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"newviews"]; 
[self presentViewController:vc animated:YES completion:nil]; 

然而,當vc視圖 - 控制成功加載,用戶會點擊另一個按鈕並顯示另一個UIViewcontroller。與前面的代碼一樣,這也使用presentViewController:

現在,有2 UIViewController多數民衆贊成由presentViewController:顯示,

當一個用戶點擊按鈕,我需要這2個ViewControllers解僱。我怎樣才能做到這一點 ?

我試過下面的代碼,但它沒有工作。

[self.navigationController popToRootViewControllerAnimated:YES]; 

回答

0

您需要使用[self.parentViewController dismissViewControllerAnimated:YES],因爲您以模態方式呈現它們。關閉viewControllers時,您可以利用parentViewController屬性。

UIViewController *mainController = [UIViewController new]; 
UIViewController *vcA = [UIViewController new]; 
UIViewController *vcB = [UIViewController new]; 

// User taps button to present vcA 
[mainController presentViewController:vcA animated:YES completion:nil]; 

// User taps button to present vcB 
[vcA presentViewController:vcB animated:YES completion:nil]; 

// Dismiss both vcB and vcA 
[vcB.parentViewController dismissViewControllerAnimated:YES completion:^{ 
    [vcA.parentViewController dismissViewControllerAnimated:YES completion:^{ 
     // Now you are back on mainController 
    }]; 
}];