.delegate=self 是什么意思?
谁能解释一下 someViewController.delegate = self
和 self.delegate
的含义?他们在哪里帮助我们?
Could anyone explain the meaning of someViewController.delegate = self
and self.delegate
? Where do they help us?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代表向您发送消息。
例如:如果您使用加速度计委托,您将收到有关加速度计的消息。
如果您使用新的中微子检测委托,您将收到有关在该区域检测到的任何中微子的消息。
如果您使用 PopUps,PopUps 会向您发送消息。完成的方法是使用 PopUp 的委托。有很多很多的例子。
因此,代表们发送消息。
就是这么简单。
您可能会问,“它在哪里发送这些消息?”
答案是这样的:它将消息发送到您设置“.delegate”的地方。
当您“设置委托”时,您所做的就是说出您希望消息到达的位置。
因此,
blah.delegate = AmazingPlace 会将消息发送到“amazingPlace”。
blah.delegate = SomethingElse 会将消息发送到“somewhereElse”。
blah.delegate = self 将发送消息......给你。
很多时候,您希望消息发送给“您”,因此您只需说“blah.delegate = self”
忘记那行代码是一个非常常见的错误。
如果您忘记了这一点一行代码,你就被塞满了。这些消息无处可去,而您则绞尽脑汁试图找出问题所在。
您还必须做的其他事情:当您使用委托时,您必须事先宣布您想要使用委托。
怎么做呢?
这非常简单...
在过去使用 Objective-C 的时候...
您可以看到“BigTop”想要使用两个委托,即 ASIHTTPRequestDelegate 和 UIPopoverControllerDelegate。而“Flying”只想使用一个委托 - 它想要使用加速度计。
在 Swift 中……
如果不到处使用委托,你就无法真正在 iPhone 上做很多事情。
iOS 中随时随地都在使用委托。
一个类可能使用十几个代表是完全正常的。也就是说,您的班级将希望从十几个代表那里获取消息。
如今,使用 Swift
,您只需键入即可。
这就是你正在做的事情。代表发送消息。您必须说明您希望将消息发送到何处。通常,您希望他们转到“您”,因此在这种情况下您只需说
blah.delegate=self
即可。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...
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...
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
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
.Delegate
用于在类的两个对象之间传递/通信数据/消息。这里,tableView
(发送者)将数据/消息发送到viewController
(接收者)。考虑在自定义
viewController
中实现UITableView
的示例在这里,
UITableViewDataSource
&UITableViewDelegate
实际上是协议。不幸的是,UIKit Framework
不是开源的。但在参考了许多文章后,我将向您保证内部发生的情况。协议就像篮球教练一样,有一些要求。他/她通过使用这些要求来告诉玩家诸如类、结构、枚举
要做什么?
。但他/她自己不知道
该怎么做?
。因此,符合该协议的类或结构应该在实现扣篮的同时提供这些要求的实现。协议被称为数据源协议,那么它总是包含具有“返回类型”的所需函数,如下所示。
在自定义 viewController 中实现 UITableView
这里,
tableView
充当 Delegator(sender) 和 Delegator(sender) 的角色。viewController 对象即(自身)
作为委托(接收者)。为了在
viewController
中获取UITableView
,它应该符合这两个协议。因此,viewController 类对象已经实现了这两个协议所需的所有功能。现在
self
可以用作UITableViewDelegate
类型或UITableViewDataSource
类型,因为 Protocol 可以用作符合它的类的对象的类型。现在,
tableView
的两个属性即delegate
&dataSource
被分配给self
因为它具有相同的各自协议类型。两个协议的非可选功能均在 viewController 类对象中实现,如下所示
协议
UITableViewDelegate
函数协议
UITableViewDataSource
函数1) 当用户选择一个行,然后
tableview
(Sender) 即UItableView()
通过将数据传递给参数tableView< 来调用下面所示的
UITableViewDelegate
函数/代码> &indexPath
通过其delegate
属性驻留在viewController
对象(Receiver)中。现在,viewController 使用这些传递的数据来执行进一步的处理,例如推送或弹出到新的自定义 viewController。2)
UITableViewDatasource
协议内的函数向tableview
(Sender)提供自定义数据。tableview
通过调用 Datasource 函数并将数据传递给参数tableView
来询问viewController
对象。indexPath
通过其datasource
属性驻留在viewController
对象(Receiver)中。现在viewController
使用那些传递的数据&返回自定义数据tableview
。现在,tableview
使用这些数据在一个部分中创建“10”个单元格。 最后,整个UIKit Framework
使用 delegate & 。所有类中的数据源设计模式,例如UIApplication
、UITableView
、UICollectionView
、UITextField
和等等来进行数据通信。不幸的是,UIKit Framework
不是开源的。Delegate
is used to pass/communicate data/message b/w two objects of classes. Here,tableView
(Sender) sends data/message toviewController
(Receiver).Consider example of implementing
UITableView
in customviewController
Here,
UITableViewDataSource
&UITableViewDelegate
are actually protocols. Unfortunately,UIKit Framework
is not open source. But I will assure this what internally happens after referring many articles.Protocol is like basketball coach with some requirements in it. He/She tells players like class, struct, enum
what to do?
by using those requirements. But He/Shedoesn't knows
how to do?
by themself. So, the class or struct which conforms that protocol should provide implementation to those requirements while achieving to dunk the ball.A Protocol is said to be DataSource protocol then it always contains required functions with "return type" as shown below.
Implementing UITableView inside custom viewController
Here,
tableView
acts as Delegator(sender) &viewController object i.e (self)
as Delegate(receiver).In order to get
UITableView
inviewController
.It should to conform to both the Protocols.So,
viewController
class object has implemented all those required functions of both the protocols. Nowself
can be used either asUITableViewDelegate
type orUITableViewDataSource
type because Protocol can be used as type for an object of class which conforms to it.Now, both properties of
tableView
i.edelegate
&dataSource
are assigned toself
because its having same respective protocol types.The non-optional functions of both Protocols are implemented in
viewController
class object as belowProtocol
UITableViewDelegate
functionsProtocol
UITableViewDataSource
functions1) When the user select a row in a section then
tableview
(Sender) i.eUItableView()
calls theUITableViewDelegate
func below shown by passing data to parameterstableView
&indexPath
which resides inviewController
object(Receiver) through itsdelegate
property. NowviewController
uses those passed data to do further processes like pushing or poping to new custom viewController.2) Functions inside
UITableViewDatasource
protocol provides custom data totableview
(Sender). Thetableview
asks theviewController
object by calling Datasource functions with passing data to parameterstableView
&indexPath
which resides inviewController
object(Receiver) through itsdatasource
property. NowviewController
uses those passed data & returns custom data backtableview
. Nowtableview
uses those data to create "10" cells in a section & kind of "cell" at indexpathFinally, whole
UIKit Framework
uses delegate & datasource design patterns in all its classes such asUIApplication
,UITableView
,UICollectionView
,UITextField
& so on to communicate data. Unfortunately,UIKit Framework
is not open source.如果在任何情况下 Bourne 的答案都没有帮助.. 委托基本上是对象上的事件的反应,并且说“.delegate=self”意味着这些协议已在 self 中采用...例如.. 当发生什么时在 tableview 中选择一行是由 tableview 的委托方法“didSelectRowAtIndexPath”告知的...并且如果视图控制器有一个 tableview ..
并且“didSelectRowAtIndexPath”是在该视图控制器中定义的,然后我们才会说... tableview.delegate = self“...
而“self.anything”用于表示“任何东西”是 self 的属性。
例如。
NSString* 任何东西;
@property(nonatomic,retain) NSString* 任何东西;
然后“自我.任何东西”
If in any case Bourne's answer doesn't help .. a delegate is basically the reaction of an event on an object and saying ".delegate=self" means those protocols have been adopted in self ... for eg.. what happens when a row is selected in tableview is told by tableview's delegate method "didSelectRowAtIndexPath" ... and if a viewcontroller has a tableview ..
and "didSelectRowAtIndexPath" is defined in that viewcontroller only then we will say ... tableview.delegate = self"...
and "self.anything" is used to say that "anything" is a property of self..
for eg.
NSString* anything;
@property(nonatomic,retain) NSString* anything;
then "self.anything"