请问有谁能解释一下 someViewController.delegate = self
和 self.delegate
的含义?它们对我们有什么帮助?
请问有谁能解释一下 someViewController.delegate = self
和 self.delegate
的含义?它们对我们有什么帮助?
// old days!
@interface AppDelegate_Pad : NSObject <UIApplicationDelegate>
@interface BigTop : UIViewController <ASIHTTPRequestDelegate,
UIPopoverControllerDelegate>
@interface Flying : UIViewController <UIAccelerometerDelegate>
您可以看到 'BigTop' 想要使用两个代理,即 ASIHTTPRequestDelegate 和 UIPopoverControllerDelegate。而 'Flying' 只想使用一个代理 - 它想要使用加速度计。 class YourClass:UIViewController, SomeDelegate, AnotherDelegate
在iPhone上,如果不到处使用代理(delegates),你几乎无法做任何事情。
iOS中的所有地方都在使用代理。
一个类可能会使用十几个代理是很正常的。也就是说,你的类需要从十几个代理那里获取信息。
现在使用Swift很简单,只需键入:
blah.delegate = self
就是这样了。
所以这就是你所做的。委托发送消息。你必须说出要将消息发送到哪里。通常,你希望它们发送给“你自己”,因此在这种情况下,你只需说blah.delegate=self
。
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
}
在自定义视图控制器中实现UITableView
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
override func viewDidLoad {
tableView.delegate = self
tableView.dataSource = self
}
在这里,tableView
充当委托者(发送方),而 viewController 对象即 (self)
充当代理者(接收方)。
为了在 viewController
中获取 UITableView
。它应该符合两个协议。
因此,viewController
类对象已经实现了这两个协议所需的所有必要函数。现在,self
可以被用作 UITableViewDelegate
类型或 UITableViewDataSource
类型,因为协议可以用作类的对象类型,该类符合该协议。
现在,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
(发送方),即UItableView()
通过其delegate
属性调用下面所示的UITableViewDelegate
函数,并将数据传递给viewController
对象(接收方)中的tableView
和indexPath
参数。现在,viewController
使用这些传递的数据进行进一步的处理,例如推送或弹出到新的自定义视图控制器。
tableView.delegate?.tableView(UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
2) UITableViewDatasource
协议中的函数提供自定义数据给tableview
(发送方)。通过调用带有传递数据到参数tableView
和indexPath
的Datasource函数,tableview
向viewController
对象(接收方)询问。这些参数位于viewController
对象的datasource
属性中。现在,viewController
使用传递的数据并返回自定义数据给tableview
。现在,tableview
使用这些数据在一个部分中创建"10"个单元格,并在indexpath上创建一种"cell"
tableView.dataSource?.tableView(UITableView, numberOfRowsInSection section: Int) -> returns "10"
tableView.dataSource?.tableView(UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> returns "cell"
最后,整个UIKit Framework
在其所有类中都使用委托和数据源设计模式,例如UIApplication
、UITableView
、UICollectionView
、UITextField
等等,以便进行数据通信。不幸的是,UIKit Framework
不是开源的。
如果 Bourne 的回答在任何情况下都没有帮助... 代理基本上是对象事件的反应,而 ".delegate=self" 表示这些协议已经在 self 中被采用了... 例如... 当在 tableview 中选择一行时会发生什么由 tableview 的代理方法 "didSelectRowAtIndexPath" 告诉我们... 如果一个视图控制器有一个 tableview... 并且 "didSelectRowAtIndexPath" 只在该视图控制器中定义,那么我们就会说... tableview.delegate = self"... 而 "self.anything" 用于表示 "anything" 是 self 的属性... 例如。 NSString* anything; @property(nonatomic,retain) NSString* anything;
那么就是 "self.anything"
self
时会发生什么? - Bennyblah
是一些数据管理器的对象。假设你有一个 WeatherManager 结构体来处理 URL 请求和解析 JSON 数据。你必须在你的 UIViewController 类中声明weather = WeatherManager()
,该类还实现了协议 WeatherManagerDelegate 中声明的方法(按照惯例将这些方法放在与对应数据相对应的结构体所在的同一文件中,即 WeatherManager)。因此,blah
变成了weather
,因此weather.delegate = self
。 - Eduardo Perez