xcode 无法从笔尖实例化 UIView。“警告:无法加载任何 Objective-C 类信息”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34581479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot instantiate UIView from nib. "warning: could not load any Objective-C class information"
提问by EBDOKUM
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:
我得到“无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。” 初始化此类的实例时控制台中的警告:
@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...)"
执行在let view = nib.instantiateWithOwner...
“线程 1:EXC_BAD_ACCESS(code=2...)”时停止
What could be the possible reason behind this?
这背后的可能原因是什么?
回答by EBDOKUM
Found the solution. It all comes to understanding of how xibs work.
找到了解决办法。这一切都是为了理解xibs的工作原理。
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.
我所做的是为视图和文件的所有者设置了类,并从视图而不是文件的所有者连接了所有出口。
回答by Paul.s
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 :/
这似乎是您在实例化视图方面走了很长一段路。您有一个类的视图,SystemMessage
它实例化了一个名称笔尖,SystemMessage
然后将其作为视图插入:/
The simplest way to do this is to set the root view in your Xib to be of type SystemMessage
最简单的方法是将 Xib 中的根视图设置为类型 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
这只是为您提供了一种简单的方法来从代码中实例化您的视图SystemMessage.loadViewFromNib()
。File's Owner
在这种情况下可能设置不正确