我刚刚创建了名为 PopupViewForViewMoreDetail
的自定义 UIView
,我想在我的 ViewController 中添加这个自定义 View ,但每次都出现以下错误
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<0x7f8155f2e430> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainView.'
如果我删除 mainView 的 IBOutlet 那么它将显示另一个变量的错误。
下面是我的代码
import UIKit
class PopupViewForViewMoreDetail: UIView {
@IBOutlet var darkBGView: UIView!
@IBOutlet var outerView: UIView!
@IBOutlet var mainView: UIView!
@IBOutlet var btnClose: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我正在将这个 View 添加到我的 viewController 类中,如下所示。
let viewMoreDetailPopUp = Bundle.main.loadNibNamed("PopupViewForViewMoreDetail", owner: self, options: nil)?.first as! PopupViewForViewMoreDetail
self.view.addSubview(viewMoreDetailPopUp)
下面是我的IBOutlets
截图我也检查了下面的答案,但没有帮助我。
请指导我的代码哪里出错了?
最佳答案
试试这个:
step 1:
let viewMoreDetailPopUp = UINib(nibName: "PopupViewForViewMoreDetail", bundle: nil).instantiate(withOwner: self, options: nil).first as! PopupViewForViewMoreDetail
step 2: make sure you're setting class for the view instead of File's Owner
Step 3 : Remove all outlets and re-outlet again
关于ios - 加载 XIB 此类与键的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056322/