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

我只是一个虾纸丫 提交于 2019-12-01 11:52:05

Found the solution. It all comes to understanding of how xibs work.

What I did was that I set class for both view and File's Owner and connected all the outlets from the View rather than from the File's owner.

This seems like you are going the long way round instantiating a view. You have a view of class SystemMessage which instantiates a nib of name SystemMessage and then inserts that as a view :/

The simplest way to do this is to set the root view in your Xib to be of type SystemMessage

Then you connect your outlets to the view that you just gave the right type

This means that you can lose have your code and end up with

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
  }

}

This just gives you an easy way to instantiate your view from code with SystemMessage.loadViewFromNib(). File's Owner is probably being set incorrectly in this instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!