I added UIView (A Class) as a xib instance to ViewController.
I want to get image from UIImagePickerController of ViewController class by action (in A Class) and return it to A Class.
I wrote up the following codes. However, I do not know how to return to A Class.
I made delegate to ViewController again but I abandoned it because I displayed various errors of delegate. [Un wrap error ..., just did not move anything ... etc].
// A Class
class A: CustomDelegate{
var delegate001: PhotoLibraryDelegate!
class func instance() -> A {
return UINib(nibName: "A", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! A
}
@IBAction func openPhotoLibrary(_ sender: Any) {
self.delegate001.selectPhoto() //I want photo in A Class XD. so make Delegate!!
}
}
@objc protocol PhotoLibraryDelegate {
func selectPhoto()
}
//ViewController Class
class ViewController: PhotoLibraryDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let a = A.instance()
override func viewDidLoad() {
super.viewDidLoad()
a.delegate006 = self
}
func selectPhoto() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let pickerView = UIImagePickerController()
pickerView.sourceType = .photoLibrary
pickerView.delegate = self
self.present(pickerView, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!
self.dismiss(animated: true)
//... Wa!? I want to return "image" to A class. OMG.
}
}
The simplest way is that you have already created the instance of A class. Now just directly assign the image to it.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!
a.imageView.image = image
self.dismiss(animated: true)
}