[英]How to dismiss two modal view controllers at once?
我在导航堆栈中有一个视图控制器A。 一个模态表示另一个控制器B,而后者又可以模态表示另一个控制器C。
当用户在CI中单击按钮时,要同时撤消C和B以返回到A。如何立即撤消B和C?
下面的代码有效,但是这样做安全吗?
let p = self.presentingViewController
self.dismiss(animated: true) {
p?.dismiss(animated: true, completion: nil)
}
将最顶层的视图控制器设置为属性。 确保B
为C
设置了最顶层的视图控制器。
class C: UIViewController {
var topmostViewController: UIViewController?
@IBAction func dismiss(_ sender: Any) {
// If topmostViewController was not set, then assume
// the presenting view controller is to be dismissed.
(topmostViewController ?? presentingViewController)?.dismiss(animated: true)
}
}
这为您提供了很大的灵活性,可以确定哪个视图控制器执行关闭操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.