2013-10-31 41 views
2

我有三個ViewControllers,它的順序是(A呈現B呈現C), 當我留在C視圖控制器中時,我必須將ViewController關閉到B視圖控制器。通過使用presentsViewController來解僱viewController

對於C的viewController,其presentingViewCOntroller爲B的viewController

當然,我可以使用

[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController 

但我不知道我還可以使用下面的方法:

[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; 

因爲C的呈現視圖控制器是B ViewController,,但是它的效果相同。 自我結構C視圖控制器同時self.presentingViewController就意味着B的ViewController,但他們也做了同樣的工作

第二個問題是,我不能使用以下辭退2的viewController陸續:

[self dismissViewControllerAnimated:YES completion:^{ 
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; 
}]; //self means C viewController 

謝謝您幫幫我!

回答

2

當然,我可以用

[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController 

這隻駁回C,你似乎想不B

但我不知道我還可以用下面的方法:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; 

是的,這工作。如有疑問,請嘗試一下。

第二個問題是,我不能使用以下辭退2的viewController陸續:

[self dismissViewControllerAnimated:YES completion:^{ 
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; 
}]; //self means C viewController 

這不是一個問題。無論如何,它不起作用的原因是,在你解僱你自己後,你的presentingViewControllernil。您需要將其存儲在臨時變量中。

UIViewController *gp = self.presentingViewController.presentingViewController; 
[self dismissViewControllerAnimated:YES completion:^{ 
    [gp dismissViewControllerAnimated:YES completion:nil]; 
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
}]; 

當然,這兩個將有不同的動畫,你需要決定你更喜歡哪一個。

+0

感謝您的第二個答案,我很抱歉,我沒有discripe第一question.I只是想知道自己和self.presenting代表不同的對象,但他們可以做同樣的工作 – passol

0

請檢查:self.presentingViewController.presentingViewController是否爲viewController。

如果沒有,我想你需要使用委託或處理程序。

在我的情況下,我使用了tabbarcontroller作爲rootViewController。當我使用self.presentingViewController.presentingViewController時,我得到了rootViewController(tabbarcontroller),它沒有響應呈現ViewControler方法。

3

見文檔:

所述呈現視圖控制器負責駁回視圖 控制器它呈現。 如果您在所呈現的視圖 控制器本身上調用此方法,它會自動將該消息轉發給呈現視圖控制器的消息

相關問題