当我尝试调用此函数时出现错误Ambiguous reference to member 'TimelineSectionHeaderView'
:
if let view = TimelineSectionHeaderView.instantiateFromNib() {
return view
}
我在这里做错了什么?
import UIKit
class TimelineSectionHeaderView: UIView {
@IBOutlet weak var dayLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
class func instantiateFromNib() -> TimelineSectionHeaderView? {
let x = UINib(nibName: "timelineSectionHeader", bundle: nil).instantiate(withOwner: nil, options: nil).first as? TimelineSectionHeaderView
print(x ?? "no value")
return x
}
}
最佳答案
您需要在下面删除此行:-
if let view = TimelineSectionHeaderView.instantiateFromNib() {
return view
}
创建一个这样的实例方法:
func instantiateFromNib() -> TimelineSectionHeaderView? {
let x = UINib(nibName: "timelineSectionHeader", bundle: nil).instantiate(withOwner: nil, options: nil).first as? TimelineSectionHeaderView
print(x ?? "no value")
return x
}
现在,在您的ViewController中,您可能已经创建了
TimelineSectionHeaderView
的出口,使用该outlet
变量,然后像这样调用它:if let view = timelineViewInstanceVariable.instantiateFromNib() {
return view
}