ios - 加载 XIB 此类与键的键值编码不兼容

标签 ios swift cocoa-touch xib

我刚刚创建了名为 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.'

如果我删除 ma​​inViewIBOutlet 那么它将显示另一个变量的错误。

下面是我的代码

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

截图

enter image description here

我也检查了下面的答案,但没有帮助我。

What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"

请指导我的代码哪里出错了?

最佳答案

试试这个:

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 Ownerenter image description here

enter image description here

Step 3 : Remove all outlets and re-outlet again

关于ios - 加载 XIB 此类与键的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056322/

相关文章:

swift - 切换到 Xcode 6 Beta 5 后,所有 IBOutlets 都变为 nil

允许用户在屏幕上绘图并保存为图像的 iOS SDK 库?

ios - iOS8 中的应用内购买收据验证

ios - 使用内部框架本地化 iOS 应用程序

ios - 如何在SKSpriteNode中 "center"SKTexture

ios - swift 用户界面 : Input Field Keyboard animation causing other views in tab view to animate

ios - 如何删除两个 UICollectionView 列之间的边距

objective-c - 使用 Objective-C 的 JSON 框架解析嵌套的 JSON 对象

ios - 获取 Facebook 用户的基本信息时收到错误响应

ios - 如何使用 Xcode 7 为 iPhone 5 添加启动屏幕