我试图像这样关闭 View Controller :
func cropViewController(cropViewController: TOCropViewController!, didFinishCancelled cancelled: Bool) {
if let vc = cropViewController.parentViewController {
print("has controller")
vc.dismissViewControllerAnimated(true, completion: nil)
}
}
TOCropViewController 是 UIViewController 的子类。有时我从 self
呈现它,有时从 picker
(UIImagePicker Controller )呈现它。我试图通过访问父级来关闭它,但是 if 语句中没有任何代码正在执行。似乎找不到 Controller 。有什么想法我可能在这里做错了吗?任何帮助将非常感激!谢谢!
最佳答案
呈现的 View Controller 没有父级,它有一个呈现器。这可能会令人困惑。
您可以发送dismissViewControllerAnimated(_:completion:)
到呈现的 View Controller 以使其自行消失:
If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
或者您可以向 View Controller 询问其正在呈现的 View Controller ,并要求呈现者关闭所呈现的:
if let vc = cropViewController.presentingViewController {
print("has presenter")
vc.dismissViewControllerAnimated(true, completion: nil)
}
关于ios - 无法使用parentViewController (Swift) 关闭viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486207/