简体   繁体   中英

Assign delegate to a class using another protocol with default delegate implementation?

I have a protocol ImagePickerPresentable:

protocol ImagePickerPresentable {
   var imagePicker: UIImagePickerController? { get set }
   func presentImagePicker(withSourceType: UIImagePickerControllerSourceType) throws
   func dismissImagePicker()
   weak var delegate: ImagePickerPresentableDelegate? { get set }
}

I have an extension with default implementation for both functions.

extension ImagePickerPresentable where Self: UIViewController

ImagePickerPresentableDelegate:

protocol ImagePickerPresentableDelegate: class {
    func imagePicker(imagePicker: UIImagePickerController, didFinishPickingImage image: UIImage?, withSuccess success: Bool)
}

I tried to extend ImagePickerPresentable for default implementation of imagePicker delegate functions:

extension ImagePickerPresentable where Self: UIImagePickerControllerDelegate & UINavigationControllerDelegate {

 func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissImagePicker()
}

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// No Callback
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: editedImage, withSuccess: true)
    } else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: originalImage, withSuccess: true)
    } else {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: nil, withSuccess: false)
    }
  }
}

In the view controller that adopts the protocol I assign self as imagePicker delegate, but ImagePickerPresentable doesn't get the callback when the imagePicker finished picking media.

If I implement the image picker delegate functions directly in the view controller, I do get the callback. I could set up a function in the protocol that handles the image picker delegate function output but is it possible to get the image picker delegate to use the default implementation of the ImagePickerPresentable delegate functions directly?

Since my View Controller conforms to ImagePickerPresentable, UIImagePickerControllerDelegate, and UINavigationControllerDelegate I thought the delegate functions in ImagePickerPresentable extension would be recognized by imagePicker delegate.

您可以使用多播委托来实现此目的。

Two things to check here.

  1. Where is imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) being called? Is it being called at all?
  2. I'd add a guard statement to unwrap delegate , and return another kind of callback for an unsuccessfull unwrap.

Post more code, like an example controller, that you're using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM