Objective c .delegate=self是什么意思?

Objective c .delegate=self是什么意思?,objective-c,cocoa,delegates,protocols,delegation,Objective C,Cocoa,Delegates,Protocols,Delegation,有人能解释一下someViewController.delegate=self和self.delegate的含义吗?他们在哪里帮助我们?如果伯恩的回答没有帮助的话。。委托基本上是一个事件对一个对象的反应,并且说“.delegate=self”意味着这些协议已经在self中被采用。。。例如。。tableview的委托方法“DidSelectRowatineXpath”告诉我们在tableview中选择一行时会发生什么。。。如果viewcontroller具有tableview。。 “DidSele

有人能解释一下
someViewController.delegate=self
self.delegate
的含义吗?他们在哪里帮助我们?

如果伯恩的回答没有帮助的话。。委托基本上是一个事件对一个对象的反应,并且说“.delegate=self”意味着这些协议已经在self中被采用。。。例如。。tableview的委托方法“DidSelectRowatineXpath”告诉我们在tableview中选择一行时会发生什么。。。如果viewcontroller具有tableview。。 “DidSelectRowatineXpath”是在viewcontroller中定义的,只有这样我们才会说。。。tableview.delegate=self“。。。 “self.anything”用来表示“anything”是self的属性。。 例如。 任何东西; @属性(非原子,保留)NSString*任何内容


然后“self.anything”

代表向您发送消息

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

如果你使用新的中微子探测代表,你将得到有关在该地区探测到的任何中微子的信息

如果你使用弹出式窗口,弹出式窗口会向你发送消息。这样做的方式是通过弹出式窗口的委托。有很多很多例子

因此,代表们发送信息

就这么简单

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

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

当您“设置代理”时,您所做的是说出消息要发送到的位置

因此,

blah.delegate=amazingPlace将向“amazingPlace”发送消息

blah.delegate=somewhere将消息发送到“somewhere”

blah.delegate=self将向您发送消息……

通常情况下,您希望消息传递给“您”,所以您只需说“blah.delegate=self”

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

如果你忘记了那行代码,你就被塞满了。这些消息哪儿也找不到,你就只能挠头试图找出哪里出了问题

你还必须做一些事情:当你使用委托时,你必须事先宣布,你想使用委托

怎么做

这很容易

在过去的客观公正的时代

// old days!
@interface AppDelegate_Pad : NSObject <UIApplicationDelegate>
@interface BigTop : UIViewController <ASIHTTPRequestDelegate,
                                        UIPopoverControllerDelegate>
@interface Flying : UIViewController <UIAccelerometerDelegate>
如果不到处使用代理,你就无法在iPhone上做很多事情

在iOS中,代理可以随时随地使用

一个类可能使用十几个委托是完全正常的。也就是说,您的类将希望从十几个委托获取消息

现在使用Swift,你只需打字

  blah.delegate = self
这就是全部

这就是你要做的。代理发送消息。你必须说你希望消息传递到哪里。通常,你希望消息传递给“你”,所以在这种情况下,你只需说
废话。代理=self


希望有帮助。

委托
用于通过两个类的对象传递/传递数据/消息。在这里,
表视图
(发送方)将数据/消息发送到
视图控制器
(接收方)。 考虑在自定义<代码> VIEW控制器中实现<代码> UITababyVIEW/COD>的例子 在这里,
UITableViewDataSource
UITableViewDelegate
实际上是协议。不幸的是,
UIKit框架
不是开源的。但在参考了许多文章之后,我将保证这一点

协议就像篮球教练一样,里面有一些要求。他/她通过使用这些要求告诉球员,比如class、struct、enum
该做什么?
。但是他/她
自己不知道如何做?
。因此,符合该协议的类或结构应该提供这些要求的实现我想扣篮

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
充当委托人(发送者)和
viewController对象,即(自我)
充当委托人(接收者)

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

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

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

协议
UITableViewDelegate
功能 协议
UITableViewDataSource
函数 1) 当用户在节中选择一行时,
tableview
(发送方),即
UItableView()
调用下面显示的
UITableViewDelegate
函数,方法是将数据传递到
tableview
indexPath
参数,该参数位于
viewController
对象(接收方)中通过其
delegate
属性。现在
viewController
使用这些传递的数据执行进一步的过程,如推送或弹出到新的自定义viewController

tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
2)
UITableViewDatasource
协议中的函数向
tableview
(发送方)提供自定义数据。
tableview
通过调用数据源函数,向驻留的参数
tableview
indexPath
传递数据,请求
viewController
对象
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
// Do further processes like pushing or poping another viewController
}
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")
 }
tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"

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