拾取图像后如何执行Segue

时间:2019-04-16 19:22:53

标签: ios swift xcode segue

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

我已经检查了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)
        }
    }

程序将图像设置为图像视图,并在“执行”功能之后进入该打印语句,但是不会执行segue。

4 个答案:

答案 0 :(得分:1)

in storyboard select the view controller which has the imageview, at the top there is yellowcircle right click drag from there to the view controller you want to segue to and give its identifier an ID = “segueID” then edit your didfinishpicking method like this

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

答案 1 :(得分:0)

您尝试过吗:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

以@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)