4

我有三个 ViewController,它的顺序是(A 呈现 B,呈现 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

谢谢你的帮助!

4

3 回答 3

4

当然,我可以使用

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

这只会解雇C,而不是B您似乎想要的。

但我想知道我也可以使用以下方法:

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

是的,这行得通。如有疑问,请尝试一下。

第二个问题是我不能使用以下方法连续关闭两个 viewController:

[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];
}];

当然,两者会有不同的动画,你需要决定你喜欢哪一个。

于 2013-10-31T02:24:36.160 回答
4

见文档:

呈现视图控制器负责关闭它呈现的视图控制器。如果您在呈现的视图 控制器本身上调用此方法,它会自动将消息转发到 呈现的视图控制器。

于 2015-10-27T16:07:34.057 回答
0

请检查:self.presentingViewController.presentingViewController 是否为 A viewController。

如果没有,我认为您需要使用委托或处理程序。

在我的例子中,我使用了一个 tabbarcontroller 作为 rootViewController。当我使用 self.presentingViewController.presentingViewController 时,我得到了 rootViewController (tabbarcontroller),它对 presentingViewControler 方法没有响应。

于 2013-10-31T02:36:58.013 回答