[英]Instantiating UIView from xib/nib using Swift
I get an error Ambiguous reference to member 'TimelineSectionHeaderView'
when I try to call this function like: 尝试按以下方式调用此函数时,收到错误的
Ambiguous reference to member 'TimelineSectionHeaderView'
:
if let view = TimelineSectionHeaderView.instantiateFromNib() {
return view
}
What am I doing wrong here? 我在这里做错了什么?
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
}
}
You need to remove this line below:- 您需要在下面删除此行:-
if let view = TimelineSectionHeaderView.instantiateFromNib() {
return view
}
Create an instance method like this:- 创建一个这样的实例方法:
func instantiateFromNib() -> TimelineSectionHeaderView? {
let x = UINib(nibName: "timelineSectionHeader", bundle: nil).instantiate(withOwner: nil, options: nil).first as? TimelineSectionHeaderView
print(x ?? "no value")
return x
}
Now inside your ViewController you might have created the outlet of TimelineSectionHeaderView
use that outlet
variable and then invoke it like this:- 现在在您的ViewController中,您可能已经使用该
outlet
变量创建了TimelineSectionHeaderView
outlet
,然后像这样调用它:
if let view = timelineViewInstanceVariable.instantiateFromNib() {
return view
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.