gpt4 book ai didi

ios - Segue 和使用 instantiateViewController 有什么区别?

转载 作者:可可西里 更新时间:2023-11-01 00:56:00 24 4
gpt4 key购买 nike

segue 和 instantiateViewController 有什么区别?

我一直在尝试弄清楚如何使用 segues 将图像从一个 View Controller 发送到另一个 View Controller 和 2 个答案(Passing Image to another View Controller (Swift)How do I segue an image to another ViewController and display it within an ImageView?)都说要使用 segues 但在尝试使用 segues 时我遇到了一些问题,例如照片库关闭后第二个 View Controller 未显示或图像未显示。

继续示例

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is XferImageViewController {
print("Test: ", pickedImage.image)
let xferVC = segue.destination as? XferImageViewController
xferVC?.storedImage = pickedImage.image
}
print("WHAT IS GOING ON")
// if segue.destination is XferImageViewController {
// let xferVC = segue.destination as? XferImageViewController
// print(pickedImage.image)
// //xferVC?.storedImage = pickedImage.image
// xferVC?.storedImage = pickedImageVar
// }

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
pickedImage.image = image
} else {
print("Something went wrong")
}
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated:true, completion: nil)
performSegue(withIdentifier: "xferImage", sender: self)
}

实例化 View Controller 示例

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
pickedImage.image = image
} else {
print("Something went wrong")
}
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated:true, completion: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "xferImage") as! XferImageViewController
controller.storedImage = image
present(controller, animated: true, completion: nil)
}

所以在使用 InstantiateViewController 而不是 segue 之后,我得到了我想要的结果。两者有什么区别? (我确定那个 segue 标识符,尝试过 segue.destination 和 Storyboard ID 但仍然没有得到我需要的东西)有可能我不知道如何在照片库取消调用后使用 segue 但仍然想知道区别。

最佳答案

问题在于,当您启动转场时,转场对 View Controller 层次结构的状态有特殊要求。你必须推迟 performSegue(withIdentifier:sender:) 直到 dismiss 完成,即将它放入 dismiss 的完成句柄中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
pickedImage.image = image
} else {
print("Something went wrong")
}

dismiss(animated: true) {
self.performSegue(withIdentifier: "xferImage", sender: self)
}
}

以上对我来说效果很好。

顺便说一句,你可以简化你的prepare(for:sender:)实现:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let xferVC = segue.destination as? XferImageViewController {
xferVC.storedImage = pickedImage.image
}
}

两个有些不相关的观察结果:

  1. 如果 pickedImage 是一个 ImageView ,我建议将它重命名(并更新 Storyboard中的导出)为 pickedImageView。避免混淆 UIImage 属性和 UIImageView 导出是一个很好的约定。

  2. 这是一个更小的观察,但在 Model-View-Controller 中设计时,您通常不想依赖 UIKit 对象,如 UIImageView 来保存模型对象,即所选图像。它暗示了“ View ”对象和“模型”对象之间的概念混淆。另外,如果当前 View Controller 没有 UIImageView 怎么办?

    我个人建议将所选图像存储在单独的 UIImage? 属性中:

    private var selectedImage: UIImage?

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    selectedImage = image

    // if you also want to update a `UIImageView` in the current
    // view controller, fine, do that, but it shouldn't be confused
    // with the "model".
    //
    // pickedImageView.image = image
    } else {
    print("Something went wrong")
    }

    dismiss(animated: true) {
    self.performSegue(withIdentifier: "xferImage", sender: self)
    }
    }

    和:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let xferVC = segue.destination as? XferImageViewController {
    xferVC.storedImage = selectedImage
    }
    }

关于ios - Segue 和使用 instantiateViewController 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48219534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com