What is meant by .delegate=self?

27,262

Delegates send messages to you.

For example: if you use the accelerometer delegate, you will get messages about the accelerometer.

If you use that new neutrino-detection delegate, you will get messages about any neutrinos detected in the area.

If you use PopUps, PopUps send you messages. And the way that is done, is with the PopUp's delegate. There are many, many examples.

So, delegates send messages.

It's that simple.

You might ask, "WHERE does it send these messages?"

The answer is this: it sends the messages to where you set the ".delegate" thingy.

When you "set the delegate," what you are doing is saying where you want the messages to go.

Hence,

blah.delegate = amazingPlace will send the messages to "amazingPlace".

blah.delegate = somewhereElse will send the messages to "somewhereElse".

blah.delegate = self will send the messages ...... to you.

Very often, you want the messages to come to "you", so you just say "blah.delegate = self"

It is a very common mistake, to forget that line of code.

If you forget that line of code, you are stuffed. The messages go nowhere, and you are left scratching your head trying to figure out what went wrong.

Something else you have to do: when you use a delegate, you have to announce beforehand, that, you want to use the delegate.

How to do that?

It's very easy...

In the old days with Objective-C...

// old days!
@interface AppDelegate_Pad : NSObject <UIApplicationDelegate>
@interface BigTop : UIViewController <ASIHTTPRequestDelegate,
                                        UIPopoverControllerDelegate>
@interface Flying : UIViewController <UIAccelerometerDelegate>

You can see that 'BigTop' wants to use two delegates, namely the ASIHTTPRequestDelegate and the UIPopoverControllerDelegate. Whereas 'Flying' only wants to use one delegate - it wants to use the accelerometer.

In Swift...

 class YourClass:UIViewController, SomeDelegate, AnotherDelegate

You can't really do much on the iPhone without using delegates all over the place.

Delegates are used everywhere and all the time in iOS.

It is perfectly normal that a class might use a dozen delegates. That is to say, your class will want to get messages from a dozen delegates.

Nowadays with Swift you simply type

  blah.delegate = self

and that's all there is to it.

So that's what you're doing. Delegates send messages. You have to say where you want the messages to go. Very typically, you want them to go to "you," so in that case you simply say blah.delegate=self.

Share:
27,262

Related videos on Youtube

Markandayan P
Author by

Markandayan P

Updated on November 29, 2021

Comments

  • Markandayan P
    Markandayan P over 2 years

    Could anyone explain the meaning of someViewController.delegate = self and self.delegate? Where do they help us?

  • Fattie
    Fattie over 13 years
    Incidentally there is NOT REALLY a neutrino detection delegate. You have to write your own.
  • Benny
    Benny over 10 years
    Great answer. Could you give an example, on the flip side, when it would be necessary (and what it means) to set the delegate to something else? (i.e. what happens when you don't use self)
  • Fattie
    Fattie over 10 years
    Sure! You're a "house". You make a "room". The room gets delegate information from something (let's say, a "thermometer"). So essentially, IN "HOUSE", you would make the room, and then you would say "set thermometer delegate to room". OR !!!!! Simply, in the "ROOM," when it is initialised, you could say "set thermometer delegate to self"! It's exactly the same, see! Hope it helps!!
  • Fogmeister
    Fogmeister over 6 years
    Down voting is for more than just if the answer is incorrect. If you hover over the down vote button you will see a tool tip. I would suggest answering questions that are more recent and don’t have answers already. You can favourite tags and it will filter the list of questions to be more relevant to you. That way you can find newer questions to answer.
  • Greg
    Greg over 6 years
    What a fantastic explanation - it reminds me of the best teachers I ever had. If you have a moment to spare then I have a new question on a related topic stackoverflow.com/q/49246176/2348597 I'd welcome your response.
  • uuuuuu
    uuuuuu over 3 years
    and what does blah represent?
  • Eduardo Perez
    Eduardo Perez over 2 years
    @uuuuuu waking up an older thread but to answer your question, in an example blah is the object for some manager of data. Let's say you have a WeatherManager struct that handles url requests and parsing json data. You'd have to declare that weather = WeatherManager() in your UIViewController class which also implements the methods declared in the protocol WeatherManagerDelegate (& by convention place these within the same file holding the struct that corresponds to the data being handled, which in this case is WeatherManager). So, blah becomes weather and thus weather.delegate = self