0

I cannot understand what Delegates are used for in swift

I tried to search in any possible site what a delegate is and for what it is used for but I cannot completely grasp the meaning of them.

f.e. in this code

extension SignUpViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    }
}

what are the delegates assigned to the ViewController supposed to do? and how can I understand how to use them?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Matteo
  • 59
  • 1
  • 6
  • If I were,I'd follow [this answer](https://stackoverflow.com/a/31934786/5175709). Get it to work. Then start breaking it, let it create errors and try to understand those errors. Why it breaks and how to fix them. Cuz that's exactly what I did 3 years ago. You'll learn a lot by doing this simple exercise. – mfaani Jun 22 '19 at 23:22

2 Answers2

3

I like to think with an analogy:

There will be a "speaker" and a "listener".

UIImagePickerController is already configured to "speak" when events happen, so when something like didFinishPickingMediaWithInfo happens, it "speaks" .

In your scenario, the UIViewController should be the "listener", this is configured with:

picker.delegate = self, assuming UIViewController implements UIImagePickerControllerDelegate.


In other words, make the UIViewController the "listener" for when the picker "speak".

And everything that the picker "speak", will be "listened" via UIImagePickerControllerDelegate by whoever is "listening".


Note: This is very superficial and abstract, of course it goes much beyond this, but I'm considering the OP situation.

Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
  • 1
    Good example. To add onto this: while it's configured to speak, its listener is unknown, the speaker never itself initializes the listener. It's merely a placeholder that gets assigner **later**. Nothing is listening until you do `picker.delegate = self`. It's like telling the speaker, here is your listener. Obviously the reason the listener isn't specified until then is because the listen can be _anything_. It could be a `UIViewController`, `SomeCustomizedClass`. 1/2 – mfaani Jun 22 '19 at 23:00
  • 1
    All that it needs to do is adopt **and** conform. Adopt is to do: `: UIImagePickerControllerDelegate`. Conform is to include its function ie implement: `func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { ` 2/2 – mfaani Jun 22 '19 at 23:04
2

A delgate is a connection between 2 data types commonly vcs , that enable 1 of them to send data to the other by calling a method from the protocol that the receiver is conforming to in you example you set

let picker = UIImagePickerController()
picker.delegate = self  // connection here 

above receiver is SignUpViewController instance and sender is UIImagePickerController instance after the user picks a photo/video the picker calls didFinishPickingMediaWithInfo which is 1 of the UIImagePickerControllerDelegate methods , this what happens under the hood of

class UIImagePickerController { // look for it in framework visible part
   weak var delegate:UIImagePickerControllerDelegate?
   ....
   delegate?.imagePickerController(self,didFinishPickingMediaWithInfo:info)
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87