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

我与影子孤独终老i 提交于 2019-12-01 09:16:50

问题


I get "could not load any Objective-C class information. This will significantly reduce the quality of type information available." warning in the console while initializing an instance of this class:

@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 
    }


}

Execution stops on line let view = nib.instantiateWithOwner... with "Thread 1: EXC_BAD_ACCESS(code=2...)"

What could be the possible reason behind this?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/34581479/cannot-instantiate-uiview-from-nib-warning-could-not-load-any-objective-c-cla

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