在Swift中的ViewController中符合协议 [英] Conform to protocol in ViewController, in Swift
问题描述
尝试在Swift UIViewController子类中符合UITableViewDataSource和UITableViewDelegate。
Trying to conform to UITableViewDataSource and UITableViewDelegate inside a Swift UIViewController subclass.
class GameList: UIViewController {
var aTableView:UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
aTableView.delegate = self
aTableView.dataSource = self
self.view.addSubview(aTableView)
//errors on both lines for not conforming
}
}
文档说你应该遵循:
之后的类
行,但这通常是超类所在的位置。另一个:
不起作用。在超类之后使用逗号分隔列表也不起作用
Docs say you should conform on the class
line after the :
but that's usually where the superclass goes. Another :
doesn't work. Using a comma separated list after the superclass also doesn't work
在下面找到答案。 class GameList:UIViewController,UITableViewDataSource,UITableViewDelegate {
还必须采用每种协议的所有必需方法,我不是最初没做。
Also must adopt all required methods of each protocol, which I wasn't initially doing.
推荐答案
您使用逗号:
class GameList: UIViewController, UITableViewDelegate, UITableViewDataSource {
// ...
}
但是要意识到超类必须是逗号分隔列表中的第一项。
But realize that the super class must be the first item in the comma separated list.
如果你没有采用所有必需的方法协议会有编译器错误。你必须得到所有必需的方法!
If you do not adopt all of the required methods of the protocol there will be a compiler error. You must get all of the required methods!
这篇关于在Swift中的ViewController中符合协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!