我正在呈现两个视图控制器,如此
self.presentViewController(choosePlace, animated: true, completion: nil)
self.presentViewController(shareController, animated: true, completion: nil)
我想解雇他们两个:
println("parent \(self.parentViewController)")
self.dismissViewControllerAnimated(true, completion: {
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
})
self.parentViewController总是为零。我怎么能同时解雇两个?
答案 0 :(得分:4)
你可以:
self.dismissViewControllerAnimated(true, completion: {
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
})
因此,请确保按顺序呈现视图控制器:
parentViewController - > choosePlace - > shareController
(箭头表示“self.presentViewController”)
答案 1 :(得分:1)
如果Michael Voline的解决方案不起作用,请尝试以下代码。
let customViewController = self.presentingViewController as? CustomViewController
self.dismiss(animated: true) {
customViewController?.dismiss(animated: true, completion: nil)
}
答案 2 :(得分:1)
您可以在视图控制器的功能“ viewWillDisappear”期间引用呈现的视图控制器(即,ShareController在其离开作用域之前就关闭了choosePlace)。
//place the below in shareController
override func viewWillDisappear(_ animated: Bool) {
self.presentingViewController?.dismiss(animated: false, completion: nil)
}
答案 3 :(得分:0)
快捷键4
在尝试Michael Voline的答案时,我遇到了一些问题。
作为对答案的评论,它对我不起作用。这是因为presentingViewController
为零。
因此,我们需要在另一个属性中设置presentingController
,但是如果在viewDidLoad
private var presentingController: UIViewController?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
presentingController = presentingViewController
}
然后
dismiss(animated: false, completion: {
self.presentingController?.dismiss(animated: false)
})
我将动画设置为虚假,以便用户在关闭的几秒钟内不会看到呈现视图控制器的UI。