ios - 使用 presentingViewController 关闭 viewController

标签 ios iphone ios5

我有三个 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

这不是问题。不管怎样,它不起作用的原因是在你解雇自己之后,你的 presentingViewControllernil。您需要将其存储在一个临时变量中。

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/

相关文章:

ios - 在关闭 View 时,iAds 在 ios 5 ipad 中变为黑色。(深黑色)

javascript - 在 iOS 应用程序中嵌入 JavaScript 引擎

ios - 如何在 UIwebview 上播放 vimeo 视频而不是全屏 ios swift 2.0

iphone - 限制在数字文本字段中复制粘贴字符串

iphone - 点击导航后退按钮时会调用什么方法-IOS?

ios - 没有自动布局,如何在我的 iPhone 应用程序中使用处理 3.5 英寸屏幕和 4 英寸屏幕的单个 iOS Storyboard文件

ios - 使用自动布局的可扩展 UITableView 单元会导致 UIViewAlertForUnsatisfiableConstraints

iphone - 如何在 quartz 2d 中绘制 alpha < 1 的线

iphone - 从 NSDictionary 中检索数据

在 iOS5 中未调用 UIViewController 设备旋转委托(delegate)方法