搬运stack overflow上一个讲解delegate清晰的回答

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值