segue和instantiateViewController有什么区别?
我一直在尝试使用segue将图像从一个视图控制器发送到另一个视图控制器,两个答案(Passing Image to another View Controller (Swift) 和 How do I segue an image to another ViewController and display it within an ImageView?)都说要使用segue,但是当尝试使用segue时,我遇到了一些问题,比如照片库关闭后第二个视图控制器没有显示出来,或者图像没有显示出来。
Segue示例
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)
}
实例化视图控制器示例
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,但仍然无法得到我需要的结果)可能是因为我不知道如何在照片库dismiss调用后使用segue,但我仍然想知道它们的区别。