What is the difference between segue and instantiateViewController?
I've been trying to figure out how to use segues to send an image from one view controller to another and 2 answers (Passing Image to another View Controller (Swift) and How do I segue an image to another ViewController and display it within an ImageView?) both say to use segues but when trying to use segues I encountered a few problems like the second view controller not showing up after the photo library dismissed or that the image was not showing up.
Segue example
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)
}
InstantiateViewController Example
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)
}
So after using InstantiateViewController instead of segue I got the results that I wanted. What is the difference between the two? (I made sure that that segue identifier, tried segue.destination and storyboard ID but still wasn't getting what I needed) It is possible that I don't know how to use segue after the photo library dismiss call but still want to know the difference.