什么意思是.delegate = self? [英] What is meant by .delegate=self?

查看:229
本文介绍了什么意思是.delegate = self?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释 someViewController.delegate = self self.delegate 的含义?他们在哪里帮助我们?

解决方案

代表发送消息给你。



例如:如果您使用加速度计委托,您将收到有关加速度计的消息。



如果您使用新的中微子检测代表,您将收到有关在该地区检测到的任何中微子的消息。



如果您使用PopUps,PopUps会向您发送消息。而完成的方式就是使用PopUp的代表。有很多很多例子。



所以,代表们发送消息。



这很简单。 >

您可能会问:发送这些消息在哪里?


$ b $答案是这样的:它发送消息到你设置.delegate的东西。



当你设置委托时,你是什么做的就是说你想要的消息。



因此,



blah.delegate = amazingPlace会发送消息到amazingPlace。



blah.delegate = somewhereElse会将消息发送到somehereElse。



blah.delegate = self会发送消息...... 给你



很多时候你想要的消息来到你,所以你只是说blah.delegate = self



这是一个很常见的错误,忘记了那条代码。



如果你忘记了这行代码,你就是茧d。消息走到无处,你会刮伤你的头脑,想弄清楚出了什么问题。



你还有什么需要做的:你使用一个委托,你必须说你想使用委托。在以前的目标c中,你只要这样做...

  @interface AppDelegate_Pad:NSObject< UIApplicationDelegate> 
@interface BigTop:UIViewController< ASIHTTPRequestDelegate,UIPopoverControllerDelegate>
@interface Flying:UIViewController< UIAccelerometerDelegate>

您可以看到BigTop想要使用两个代理,即ASIHTTPRequestDelegate和UIPopoverControllerDelegate。而飞行只想使用一个代表 - 它想使用加速度计。



在Swift 中,这不容易 - 你只需要一个逗号然后协议:

  class YourClass:UIViewController,SomeDelegate,AnotherDelegate 

你不能在iPhone上做太多的工作,而不必在所有地方使用代表。



一个类可能会使用十几个代表,这是非常正常的。



在上面的飞行示例中,飞行代码中的某个地方必须说
[[UIAccelerometer sharedAccelerometer] setDelegate:self];



在BigTop的情况下,必须将代理设置为ASIHttpRequests的某个地方,

  ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; 
[请求setPostValue:gid forKey:@gid];
[request setPostValue:nom forKey:@nom];
[request setDelegate:self];

(不要在过去的日子里用objective-c, x .delegate = self [x setDelegate:self] 完全相同。现在使用Swift ,只需输入 x.delegate = self ,这就是它的一切。)



所以这就是你在做什么。代表发送邮件。你必须说你想要消息的。很通常,你希望他们去你,所以在这种情况下,你只需说 x.delegate = self



希望有帮助。


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

解决方案

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.

这篇关于什么意思是.delegate = self?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆