ios - 无法使用parentViewController (Swift) 关闭viewController

标签 ios swift

我试图像这样关闭 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/

相关文章:

ios - iOS中弹出窗口的新 View Controller 或同一 View Controller 中的新 View

ios - 从现有应用程序中排除 iPad 支持

ios - 在自定义 UITableViewCell 的标签上添加详细文本

ios - UIPageViewController 索引错误

ios - 在闪烁按钮上,它变为浅色

ios - 从 iOS 上的音乐库中获取所有歌曲

ios - 如何设置图像大小以提高 OCR 输出?

ios - 无法将类型 'UserFile' 的值转换为预期参数 'User'

iOS UITextField 快速下划线样式

ios - Swift:在 Tableview 的特定单元格中添加子层而不是每个单元格