.delegate = self是什么意思?

时间:2022-10-18 20:44:55

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

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

3 个解决方案

#1


83  

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.

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

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.

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

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 = amazingPlace会将消息发送到“amazingPlace”。

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

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

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"

很多时候,你希望消息来到“你”,所以你只要说“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...

你需要做的其他事情:当你使用委托时,你必须说你想要使用委托。在Objective-c的旧时代,你只需这样做......

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

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

In Swift, it couldn't be easier - you just have a comma then the protocols:

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

 class YourClass:UIViewController, SomeDelegate, AnotherDelegate

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

如果不在整个地方使用代表,你就无法在iPhone上做太多事情。

Delegates are used everywhere and always in iOS.

代表随处可见,总是在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];

在上面的“飞行”的例子中,在“飞行”代码的某处,它必须说[[UIAccelerometer sharedAccelerometer] setDelegate:self];

In the case of BigTop, it would have to set the delegate somewhere for the ASIHttpRequests,

对于BigTop,它必须在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].)

(不要忘记过去使用objective-c,x.delegate = self与[x setDelegate:self]完全相同。)

Nowadays with Swift you simply type

现在有了Swift,你只需输入即可

  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.

这就是你正在做的事情。代表发送消息。你必须说出你想要消息的去向。通常,你希望他们去“你”,所以在这种情况下,你只需说x.delegate = self。

Hope it helps.

希望能帮助到你。

#2


-1  

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.

Delegate用于传递/传递数据/消息b / w两个类对象。这里,tableView(Sender)将数据/消息发送到viewController(Receiver)。考虑在自定义viewController中实现UITableView的示例在这里,UITableViewDataSource和UITableViewDelegate实际上是协议。不幸的是,UIKit Framework不是开源的。但是我会在参考很多文章之后向我保证内部会发生什么。

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.

一个协议被称为DataSource协议,然后它总是包含所需的“返回类型”函数,如下所示。

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

Implementing UITableView inside custom viewController

在自定义viewController中实现UITableView

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

这里,tableView充当Delegator(发送者)和viewController对象,即(self)作为Delegate(接收者)。

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

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

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.

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

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

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

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.

1)当用户在一个部分中选择一行时,tableview(发送者)即UItableView()通过将数据传递给参数tableView和indexPath来调用下面显示的UITableViewDelegate函数,该参数通过其delegate属性驻留在viewController对象(Receiver)中。现在,viewController使用那些传递的数据来执行进一步的处理,例如推送或弹出到新的自定义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

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

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.

最后,整个UIKit Framework在其所有类中使用委托和数据源设计模式,如UIApplication,UITableView,UICollectionView,UITextField等来传递数据。不幸的是,UIKit Framework不是开源的。

#3


-3  

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;

如果在任何情况下Bourne的答案都没有帮助..委托基本上是一个事件对一个对象的反应,并说“.delegate = self”意味着那些协议已被自我采用...例如......当发生什么时发生的事情tableview的委托方法“didSelectRowAtIndexPath”告诉tableview中的一行...如果一个viewcontroller有一个tableview ..并且在该viewcontroller中定义了“didSelectRowAtIndexPath”,那么我们会说... tableview.delegate = self“ ...和“self.anything”用于表示“任何东西”是自我的属性...例如.NSString *任何东西; @property(非原子,保留)NSString *任何东西;

then "self.anything"

#1


83  

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.

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

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.

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

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 = amazingPlace会将消息发送到“amazingPlace”。

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

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

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"

很多时候,你希望消息来到“你”,所以你只要说“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...

你需要做的其他事情:当你使用委托时,你必须说你想要使用委托。在Objective-c的旧时代,你只需这样做......

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

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

In Swift, it couldn't be easier - you just have a comma then the protocols:

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

 class YourClass:UIViewController, SomeDelegate, AnotherDelegate

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

如果不在整个地方使用代表,你就无法在iPhone上做太多事情。

Delegates are used everywhere and always in iOS.

代表随处可见,总是在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];

在上面的“飞行”的例子中,在“飞行”代码的某处,它必须说[[UIAccelerometer sharedAccelerometer] setDelegate:self];

In the case of BigTop, it would have to set the delegate somewhere for the ASIHttpRequests,

对于BigTop,它必须在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].)

(不要忘记过去使用objective-c,x.delegate = self与[x setDelegate:self]完全相同。)

Nowadays with Swift you simply type

现在有了Swift,你只需输入即可

  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.

这就是你正在做的事情。代表发送消息。你必须说出你想要消息的去向。通常,你希望他们去“你”,所以在这种情况下,你只需说x.delegate = self。

Hope it helps.

希望能帮助到你。

#2


-1  

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.

Delegate用于传递/传递数据/消息b / w两个类对象。这里,tableView(Sender)将数据/消息发送到viewController(Receiver)。考虑在自定义viewController中实现UITableView的示例在这里,UITableViewDataSource和UITableViewDelegate实际上是协议。不幸的是,UIKit Framework不是开源的。但是我会在参考很多文章之后向我保证内部会发生什么。

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.

一个协议被称为DataSource协议,然后它总是包含所需的“返回类型”函数,如下所示。

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

Implementing UITableView inside custom viewController

在自定义viewController中实现UITableView

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

这里,tableView充当Delegator(发送者)和viewController对象,即(self)作为Delegate(接收者)。

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

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

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.

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

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

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

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.

1)当用户在一个部分中选择一行时,tableview(发送者)即UItableView()通过将数据传递给参数tableView和indexPath来调用下面显示的UITableViewDelegate函数,该参数通过其delegate属性驻留在viewController对象(Receiver)中。现在,viewController使用那些传递的数据来执行进一步的处理,例如推送或弹出到新的自定义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

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

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.

最后,整个UIKit Framework在其所有类中使用委托和数据源设计模式,如UIApplication,UITableView,UICollectionView,UITextField等来传递数据。不幸的是,UIKit Framework不是开源的。

#3


-3  

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;

如果在任何情况下Bourne的答案都没有帮助..委托基本上是一个事件对一个对象的反应,并说“.delegate = self”意味着那些协议已被自我采用...例如......当发生什么时发生的事情tableview的委托方法“didSelectRowAtIndexPath”告诉tableview中的一行...如果一个viewcontroller有一个tableview ..并且在该viewcontroller中定义了“didSelectRowAtIndexPath”,那么我们会说... tableview.delegate = self“ ...和“self.anything”用于表示“任何东西”是自我的属性...例如.NSString *任何东西; @property(非原子,保留)NSString *任何东西;

then "self.anything"