我有三个 ViewController,它的顺序是(A present B which presents C), 当我留在 C viewController 中时,我必须将 ViewController 解散到 B viewController。
对于C viewController,它的presentingViewCONtroller是B viewController
当然,我可以用
[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController
但我想知道我还可以使用以下方法:
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
因为C的presentingViewController是B的ViewController,但是效果是一样的。 self 表示 C ViewController 而 self.presentingViewController 表示 B ViewController,但它们也做了同样的工作
第二个问题是我不能使用下面的命令连续关闭两个 viewController:
[self dismissViewControllerAnimated:YES completion:^{
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}]; //self means C viewController
感谢您的帮助!
最佳答案
of course ,I can use
[self dismissViewControllerAnimated:YES completion:NULL];//self means C ViewController
这只会忽略 C
,而不是您想要的 B
。
But I was wondering I can also use the following method:
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
是的,这行得通。如有疑问,请尝试一下。
The second question is I cannot use the following to dismiss two viewController in succession:
[self dismissViewControllerAnimated:YES completion:^{ [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL]; }]; //self means C viewController
这不是问题。不管怎样,它不起作用的原因是在你解雇自己之后,你的 presentingViewController
是 nil
。您需要将其存储在一个临时变量中。
UIViewController *gp = self.presentingViewController.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[gp dismissViewControllerAnimated:YES completion:nil];
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}];
当然,两者会有不同的动画,你需要决定你喜欢哪个。
关于ios - 使用 presentingViewController 关闭 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19697165/