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

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

问题描述

谁能解释一下someViewController.delegate = selfself.delegate 的含义?他们在哪里帮助我们?

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

推荐答案

代表发送消息给你.

例如:如果您使用加速度计代理,您将收到有关加速度计的消息.

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.

如果您使用弹出窗口,弹出窗口会向您发送消息.完成的方式是使用 PopUp 的委托.有很多很多例子.

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.

因此,代表发送消息.

就这么简单.

您可能会问,它会将这些消息发送到哪里?"

答案是这样的:它将消息发送到您设置.delegate"的位置.东西.

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.

因此,

blah.delegate =amazingPlace 会将消息发送到amazingPlace".

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

blah.delegate = somethingElse 会将消息发送到somewhereElse".

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

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

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

很多时候,您希望消息到达您",所以您只需说blah.delegate = self"

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

忘记那行代码是一个非常常见的错误.

如果你忘记了那行代码,你就被塞满了.这些消息无处可去,而您只能挠头试图找出出了什么问题.

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.

怎么做?

这很容易...

过去使用 Objective-C...

In the old days with Objective-C...

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

你可以看到'BigTop'想要使用两个委托,即ASIHTTPRequestDelegate和UIPopoverControllerDelegate.而飞行"只想使用一个代表 - 它想使用加速度计.

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.

在 Swift 中...

In Swift...

 class YourClass:UIViewController, SomeDelegate, AnotherDelegate

如果不到处使用委托,你就无法在 iPhone 上做很多事情.

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

委托在 iOS 中随处可见.

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.

现在使用 Swift,您只需输入

Nowadays with Swift you simply type

  blah.delegate = self

仅此而已.

这就是你正在做的事情.代表发送消息.您必须说哪里您希望邮件去.非常典型的是,你希望他们去你",所以在这种情况下,你只需说 blah.delegate=self.

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.

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

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