.delegate=self 是什么意思?

发布于 2024-10-02 16:55:00 字数 105 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-10-09 16:55:00

代表向您发送消息

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

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

如果您使用 PopUps,PopUps 会向您发送消息。完成的方法是使用 PopUp 的委托。有很多很多的例子。

因此,代表们发送消息。

就是这么简单。

您可能会问,“它在哪里发送这些消息?”

答案是这样的:它将消息发送到您设置“.delegate”的地方。

当您“设置委托”时,您所做的就是说出您希望消息到达的位置。

因此,

blah.delegate = AmazingPlace 会将消息发送到“amazingPlace”。

blah.delegate = SomethingElse 会将消息发送到“somewhereElse”。

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

很多时候,您希望消息发送给“您”,因此您只需说“blah.delegate = self”

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

如果您忘记了这一点一行代码,你就被塞满了。这些消息无处可去,而您则绞尽脑汁试图找出问题所在。

您还必须做的其他事情:当您使用委托时,您必须事先宣布您想要使用委托。

怎么做呢?

这非常简单...

在过去使用 Objective-C 的时候...

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

您可以看到“BigTop”想要使用两个委托,即 ASIHTTPRequestDelegate 和 UIPopoverControllerDelegate。而“Flying”只想使用一个委托 - 它想要使用加速度计。

在 Swift 中……

 class YourClass:UIViewController, SomeDelegate, AnotherDelegate

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

iOS 中随时随地都在使用委托。

一个类可能使用十几个代表是完全正常的。也就是说,您的班级将希望从十几个代表那里获取消息。

如今,使用 Swift

  blah.delegate = self

,您只需键入即可。

这就是你正在做的事情。代表发送消息。您必须说明您希望将消息发送到何处。通常,您希望他们转到“您”,因此在这种情况下您只需说 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...

// old days!
@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...

 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 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

  blah.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 blah.delegate=self.

放飞的风筝 2024-10-09 16:55:00

Delegate 用于在类的两个对象之间传递/通信数据/消息。这里,tableView(发送者)将数据/消息发送到viewController(接收者)。
考虑在自定义 viewController 中实现 UITableView 的示例
在这里,UITableViewDataSource & UITableViewDelegate 实际上是协议。不幸的是,UIKit Framework 不是开源的。但在参考了许多文章后,我将向您保证内部发生的情况。

协议就像篮球教练一样,有一些要求。他/她通过使用这些要求来告诉玩家诸如类、结构、枚举要做什么?。但他/她自己不知道该怎么做?。因此,符合该协议的类或结构应该在实现扣篮的同时提供这些要求的实现。

protocol UITableViewDelegate {
 func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
}

协议被称为数据源协议,那么它总是包含具有“返回类型”的所需函数,如下所示。

protocol UITableViewDataSource {
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
}

在自定义 viewController 中实现 UITableView

class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()    

    override func viewDidLoad {
      tableView.delegate = self
      tableView.dataSource = self
    }

这里,tableView 充当 Delegator(sender) 和 Delegator(sender) 的角色。 viewController 对象即(自身) 作为委托(接收者)。

为了在viewController中获取UITableView,它应该符合这两个协议。

因此,viewController 类对象已经实现了这两个协议所需的所有功能。现在 self 可以用作 UITableViewDelegate 类型或 UITableViewDataSource 类型,因为 Protocol 可以用作符合它的类的对象的类型。
现在,tableView 的两个属性即delegate & dataSource 被分配给 self 因为它具有相同的各自协议类型。

两个协议的非可选功能均在 viewController 类对象中实现,如下所示

协议 UITableViewDelegate 函数

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
// Do further processes like pushing or poping another viewController
}

协议 UITableViewDataSource 函数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
 }

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
 }

1) 当用户选择一个行,然后 tableview(Sender) 即 UItableView() 通过将数据传递给参数 tableView< 来调用下面所示的 UITableViewDelegate 函数/代码> & indexPath 通过其 delegate 属性驻留在 viewController 对象(Receiver)中。现在,viewController 使用这些传递的数据来执行进一步的处理,例如推送或弹出到新的自定义 viewController。

tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

2)UITableViewDatasource协议内的函数向tableview(Sender)提供自定义数据。 tableview 通过调用 Datasource 函数并将数据传递给参数 tableView 来询问 viewController 对象。 indexPath 通过其 datasource 属性驻留在 viewController 对象(Receiver)中。现在 viewController 使用那些传递的数据&返回自定义数据tableview。现在,tableview 使用这些数据在一个部分中创建“10”个单元格。 最后,整个

tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"

tableView.dataSource?.tableView(UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> returns "cell"

UIKit Framework 使用 delegate & 。所有类中的数据源设计模式,例如 UIApplicationUITableViewUICollectionViewUITextField 和等等来进行数据通信。不幸的是,UIKit Framework 不是开源的。

Delegate is used to pass/communicate data/message b/w two objects of classes. Here, tableView(Sender) sends data/message to viewController(Receiver).
Consider example of implementing UITableView in custom viewController
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/She doesn'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.

protocol UITableViewDelegate {
 func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
}

A Protocol is said to be DataSource protocol then it always contains required functions with "return type" as shown below.

protocol UITableViewDataSource {
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
}

Implementing UITableView inside custom viewController

class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let tableView = UITableView()    

    override func viewDidLoad {
      tableView.delegate = self
      tableView.dataSource = self
    }

Here, tableView acts as Delegator(sender) & viewController object i.e (self) as Delegate(receiver).

In order to get UITableView in viewController.It should to conform to both the Protocols.

So, viewController class object has implemented all those required functions of both the protocols. Now self can be used either as UITableViewDelegate type or UITableViewDataSource type because Protocol can be used as type for an object of class which conforms to it.
Now, both properties of tableView i.e delegate & dataSource are assigned to self because its having same respective protocol types.

The non-optional functions of both Protocols are implemented in viewController class object as below

Protocol UITableViewDelegate functions

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
// Do further processes like pushing or poping another viewController
}

Protocol UITableViewDataSource functions

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
 }

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
 }

1) When the user select a row in a section then tableview(Sender) i.e UItableView() calls the UITableViewDelegate func below shown by passing data to parameters tableView & indexPath which resides in viewController object(Receiver) through its delegate property. Now viewController uses those passed data to do further processes like pushing or poping to new custom viewController.

tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

2) Functions inside UITableViewDatasource protocol provides custom data to tableview(Sender). The tableview asks the viewController object by calling Datasource functions with passing data to parameters tableView & indexPath which resides in viewController object(Receiver) through its datasource property. Now viewController uses those passed data & returns custom data back tableview. Now tableview uses those data to create "10" cells in a section & kind of "cell" at indexpath

tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"

tableView.dataSource?.tableView(UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> returns "cell"

Finally, whole UIKit Framework uses delegate & datasource design patterns in all its classes such as UIApplication, UITableView, UICollectionView, UITextField & so on to communicate data. Unfortunately, UIKit Framework is not open source.

ゝ偶尔ゞ 2024-10-09 16:55:00

如果在任何情况下 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文