为什么我们需要将委托设置为self?为什么编译器没有默认它? [英] Why do we need to set delegate to self? Why isn't it defaulted by the compiler?

查看:109
本文介绍了为什么我们需要将委托设置为self?为什么编译器没有默认它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

思考我完全理解委托的概念,我的问题是,当我们这样做时:

I think I fully understand the concept of delegation, my question is that when we do:

class someViewController : UIViewController, UITableViewDelegate{

}

曾经是不是我们不想将 tableView.delegate 设置为 self

would it ever be possible that we wouldn't want to set tableView.delegate to self?

如果没有任何机会,为什么Xcode强迫我们在这里做一些额外的工作?

If there isn't any chance then why is Xcode forcing us to do some extra work here?

如果将 tableView.delegate 设置为除 self 之外的其他东西的可能性是……那是什么?您能提供一些例子吗?

If there is a chance that tableView.delegate is set to something other than self...well what is that? Can you please provide some examples?

推荐答案

在提及 tableView.delegate = self tableView.dataSource = self 在所需的ViewController中,这意味着ViewController在说我负责实现那些委托/数据源方法,这意味着该ViewController ( self )正在努力提供所需的方法,以使tableView知道其外观/行为。

When mentioning that tableView.delegate = self or tableView.dataSource = self in the desired ViewController, that's means the ViewController is saying "I am responsible for implementing those delegation/dataSource methods", means that this ViewController (self) is taking care of providing the needed method to let tableView knows how it should looks/behaves.

提到您的问题:


我们是否有可能不想将
tableView.delegate设置为self?

would it ever be possible that we wouldn't want to set tableView.delegate to self?

实际上是可能的,但这会导致tableView显示为空tableView(其中没有行),因为没有人

Actually it's possible, but this causes to let the tableView appears as an empty tableView (no rows in it), because no one is telling it about how it should looks/behave.


是否有可能将tableView.delegate设置为其他
比自我...好吧是吗?你能提供一些例子吗?

If there is a chance that tableView.delegate is set to something other than self...well what is that? Can you please provide some examples?

是的,tableView.dataSource / delegate不必分配给包含此tableView(但我觉得它更易读和理解)。

Yes you can, tableView.dataSource/delegate not necessary to be assigned to the same Viewcontroller that contains this tableView (but I find it more readable and understandable).

例如:

在下面的代码片段中,我将tableView的dataSource分配给另一个 .swift 文件上的另一个分离的类(甚至不是UIViewController),它完全可以正常工作:

In the following code snippets, I assigning the dataSource of the tableView to another separated class (which is not even a UIViewController) on a different .swift file and it completely works fine:

import UIKit

// ViewController File
class ViewController: UIViewController {
    var handler: Handler!

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        handler = Handler()
        tableView.dataSource = handler
    }
}

处理程序类:

import UIKit

class Handler:NSObject, UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell")

        cell?.textLabel?.text = "row #\(indexPath.row + 1)"

        return cell!
    }
}

输出正常运行。

这篇关于为什么我们需要将委托设置为self?为什么编译器没有默认它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆