无法从 nib 实例化 UIView。 "warning: could not load any Objective-C class information"

Cannot instantiate UIView from nib. "warning: could not load any Objective-C class information"

我在初始化此 class 的实例时在控制台中收到 "could not load any Objective-C class information. This will significantly reduce the quality of type information available." 警告:

@IBDesignable
class SystemMessage: UIView{

    @IBOutlet weak var lbl_message: UILabel!

    var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    func setup(){
        view = loadViewFromNib()

        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view)
    }


    func loadViewFromNib() -> UIView{
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "SystemMessage", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view 
    }


}

执行在 let view = nib.instantiateWithOwner... 行停止,"Thread 1: EXC_BAD_ACCESS(code=2...)"

这背后的可能原因是什么?

找到解决方案。这一切都是为了了解 xibs 的工作原理。

我所做的是为视图和文件所有者设置 class 并连接来自视图而不是文件所有者的所有插座。

您似乎在实例化视图方面走得很远。您有一个 class SystemMessage 的视图,它实例化一个名称为 SystemMessage 的笔尖,然后将其作为视图插入:/

最简单的方法是将 Xib 中的根视图设置为 SystemMessage

类型

然后你将你的出口连接到你刚刚给出正确类型的视图

这意味着您可能会丢失代码并最终得到

import UIKit

@IBDesignable
class SystemMessage: UIView {

  @IBOutlet weak var lbl_message: UILabel!

  static func loadViewFromNib() -> SystemMessage {
    return NSBundle(forClass: self).loadNibNamed("SystemMessage", owner: nil, options: nil).first as! SystemMessage
  }

}

这只是为您提供了一种使用 SystemMessage.loadViewFromNib() 从代码中实例化视图的简单方法。 File's Owner 在这种情况下可能设置不正确