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

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

问题描述

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

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?

推荐答案

当在想要的 ViewController 中提到 tableView.delegate = selftableView.dataSource = self 时,这意味着 ViewController 说我负责实现那些委托/dataSource 方法",这意味着这个 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 给自己?

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 的同一个 Viewcontroller(但我发现它更具可读性和可理解性).

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

输出工作正常.

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

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