简体   繁体   English

无法使用parentViewController(Swift)关闭viewController

[英]Can't dismiss viewController using parentViewController (Swift)

I am trying to dismiss a view controller like so: 我试图像这样关闭视图控制器:

func cropViewController(cropViewController: TOCropViewController!, didFinishCancelled cancelled: Bool) {

        if let vc = cropViewController.parentViewController {
            print("has controller")
            vc.dismissViewControllerAnimated(true, completion: nil)
        }

    }

TOCropViewController is a subclass of UIViewController. TOCropViewController是UIViewController的子类。 Sometimes I present it from self and sometimes from picker (a UIImagePicker controller). 有时我从self呈现,有时从picker (UIImagePicker控制器)呈现。 I'm trying to dismiss it by accessing the parent however none of the code in the if statement is executing. 我试图通过访问父项来消除它,但是if语句中的任何代码都没有执行。 It can't seem to find the controller. 似乎找不到控制器。 Any ideas what I might be doing wrong here? 有什么想法我可能在这里做错了吗? Any help would be really appreciated! 任何帮助将非常感激! thanks! 谢谢!

A presented view controller doesn't have a parent, it has a presenter. 演示的视图控制器没有父级,它有一个演示者。 This can be confusing. 这可能会造成混淆。

You can send dismissViewControllerAnimated(_:completion:) to the presented view controller to have it dismiss itself: 您可以将dismissViewControllerAnimated(_:completion:)发送到所呈现的视图控制器,以使其自行关闭:

If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal. 如果您在呈现的视图控制器本身上调用此方法,则UIKit会要求呈现的视图控制器处理关闭。

Or you can ask the view controller for its presenting view controller, and ask the presenter to dismiss the presented: 或者,您可以要求视图控制器提供其呈现的视图控制器,并要求演示者关闭呈现的内容:

    if let vc = cropViewController.presentingViewController {
        print("has presenter")
        vc.dismissViewControllerAnimated(true, completion: nil)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM