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 say that you want to use the delegate. In the old days with objective-c, you just do this...
@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, it couldn't be easier - you just have a comma then the protocols:
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 always in iOS.
It is perfectly normal that a class might use a dozen delegates.
In the example of "Flying" above, somewhere in the code for "Flying" it would have to say [[UIAccelerometer sharedAccelerometer] setDelegate:self];
In the case of BigTop, it would have to set the delegate somewhere for the ASIHttpRequests,
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:gid forKey:@"gid"];
[request setPostValue:nom forKey:@"nom"];
[request setDelegate:self];
(Don't forget in the old days with objective-c, x.delegate=self
was exactly the same as [x setDelegate:self]
.)
Nowadays with Swift you simply type
x.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 x.delegate=self
.
Hope it helps.