【问题标题】:How To Perform Segue After Picking Image如何在选取图像后执行 Segue
【发布时间】:2019-04-16 19:22:53
【问题描述】:

我想在用户选择图像后(通过单击图像视图)执行转场。

我已经检查了 segue id 并成功地连接了两个视图控制器。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // Executed when we select an image from the photo library.
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

            // Sets the image to the image view (not really relevant because
            // we push the user to the DetailVC, but let's leave it).
            self.imageView.image = image

            // Closing the image picker.
            self.dismiss(animated: true, completion: nil)

            // Performing segue (It doesn't perform!).
            print("It gets to this point, but doesn't perform.")
            self.performSegue(withIdentifier: "showDetail", sender: nil)
        }
    }

程序将图像设置为图像视图,在“执行”函数之后到达该打印语句,但不执行转场。

【问题讨论】:

    标签: ios swift xcode segue


    【解决方案1】:

    在情节提要中选择具有 imageview 的视图控制器,在顶部有黄色圆圈右键单击从那里拖动到要继续的视图控制器,并为其标识符提供 ID = “segueID” 然后像这样编辑你的 didfinishpicking 方法

    if let img = info[.originalImage] as? uiimage{
    myimageview.image = img
    dismiss(animated: true) {
    self.performSegue(withIdentifier: “segueID”, sender: self)}}
    

    【讨论】:

    • 它仍然不执行segue:/
    • 你是如何在故事板中设置转场
    • 我的错。它现在运行顺利。您能否澄清一下为什么我必须将“performSegue”方法放在完成块中?
    • 据我所知,如果你在竞争处理程序中放了一些东西,它会在关闭视图控制器之前立即调用,所以如果这是一项繁重的任务,你不应该在这里做,但是因为我们想去新的视图控制器我们可以在主队列中完成
    【解决方案2】:

    我认为问题在于您在执行 segue 之前关闭了视图/视图控制器

    // Closing the image picker.
            self.dismiss(animated: true, completion: nil)
    

    【讨论】:

    • 我必须关闭图像选择器才能进入下一个视图。如果我删除此行,图像选择器将永远不会关闭。
    • 如果 segue 设置为“push”或“show”,那么它应该在导航控制器中替换它
    【解决方案3】:

    你试过了吗:

    • 将发件人设置为self?
    • 确保您的 segue 是正确的类型。 (我知道您检查了名称,如果名称错误,您可能会收到错误消息,在这种情况下,我的意思是“推送”与“显示详细信息”等)
    • 添加 prepareForSegue 函数以确保实际调用 segue

    【讨论】:

    • 是的。同样的结果:(
    • 还是不行。我什至已经尝试过使用不同类型的 segue 并更改 segue id,但还没有。
    • 您是否添加了prepareForSegue 并被调用了?
    • 我之前已经加过了,但是segue还是没有执行。激活 segue 的唯一方法是添加一个按钮。
    • 但是 prepareForSegue 是否会收到通知您的 segue 正在执行或者它永远不会被调用。这可能表明 segue 没有正确到达 UIKit。
    【解决方案4】:

    以@Loren Rogers 的回答为基础:

    // Closing the image picker.
    self.dismiss(animated: true, completion: nil)
    

    您实际上并没有解雇UIImagePickerController。就像 Loren 提到的那样,您正在解雇 ViewController。这是因为self 是对ViewController 的引用。

    self 替换为picker,如下所示:

    // Closing the image picker.
    picker.dismiss(animated: true, completion: nil)
    

    【讨论】:

    • 我把'self'改成了'picker',虽然picker被解除了,但是segue仍然没有执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2021-10-07
    • 2012-11-21
    相关资源
    最近更新 更多