问题描述
我在swift项目中加载xib文件有一些奇怪的问题。这太令人沮丧,因为我已经知道如何在Obj-C中做到这一点。但是因为swift很快,所以你不能像你那样做..:/
I have some strange problem with loading xib file in swift project. It's so frustrating because I already know how to do it in Obj-C. But since swift is swift so you can't do it like you did.. :/
所以我创建了IconTextFiled.xib和IconTextField.swift。 (扩展UITextField)在xib中我在Idenity检查器中填充字段类,在故事板中我对一些textFields执行相同的操作。所以我们很高兴只需从xib添加加载到init方法?没有。
So I have create IconTextFiled.xib and IconTextField.swift. (extends UITextField) In xib I fill field Class in Idenity inspector and in storyboard I do the same for some textFields. So we good to go just add loading from xib to init method? No.
在objc我会这样做
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IconTextField" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
所以我想如果我转换为swift它会很好。
So I thought if I translate to swift it will be good.
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let nib:NSArray = NSBundle.mainBundle().loadNibNamed("IconTextField", owner: self, options: nil)
self = nib.objectAtIndex(0)
}
但这不起作用。我不知道为什么,但它试图创建更多的对象并崩溃
But that doeasn't work. I don't know why but it try to create much more object and crash
最后我找到了扩展名
extension IconTextField {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> IconTextField? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? IconTextField
}
}
所以在ViewController中它看起来像
So in ViewController it looks like
@IBOutlet var password: IconTextField!
override func viewDidLoad() {
super.viewDidLoad()
password = IconTextField.loadFromNibNamed("IconTextField")
}
再次失败。你能告诉我你如何加载和使用xib文件吗?
And again fail. Could you tell me how you load and use xib files?
Ok以后关注Daniel anwser
Ok following after Daniel anwser
我当前的代码
class IconTextField: UITextField {
@IBOutlet var icon: UIImageView!
@IBOutlet weak var view: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSLog("initWithCoder \(self)")
NSBundle.mainBundle().loadNibNamed("IconTextField", owner: self, options: nil)
self.addSubview(view)
}
}
这两个var连接到那些视图
Those two var are connected to those views
Consoleo输出,更大,以 EXC_BAD_ACCESS
Consoleo output, was a lot bigger and end with EXC_BAD_ACCESS
2014-10-24 10:09:09.984 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7bfb0;>
2014-10-24 10:09:09.984 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7ddf0;>
2014-10-24 10:09:09.985 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7fa20;>
2014-10-24 10:09:09.985 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be814f0;>
2014-10-24 10:09:09.986 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be830c0;>
2014-10-24 10:09:10.083 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d183270;>
2014-10-24 10:09:10.084 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d187cd0;>
2014-10-24 10:09:10.084 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d189960;>
它应该只有两个initWithCoder。接缝func loadNibNamed正在调用 initWithCoder
It should be only two initWithCoder. It seams that func loadNibNamed is calling initWithCoder
推荐答案
这对我有用:
class IconTextField: UITextField {
@IBOutlet weak var view: UIView!
@IBOutlet weak var test: UIButton!
required init(coder: NSCoder) {
super.init(coder: coder)
NSBundle.mainBundle().loadNibNamed("IconTextField", owner: self, options: nil)
self.addSubview(view)
assert(test != nil, "the button is conected just like it's supposed to be")
}
}
一旦 loadNibNamed:owner:options:
被称为 view
和 test
按钮按预期连接到插座。添加笔尖的视图self的子视图层次结构使得笔尖的内容可见。
Once loadNibNamed:owner:options:
is called the view
and test
button are connected to the outlets as expected. Adding the nib's view self's subview hierarchy makes the nib's contents visible.
这篇关于Swift正确加载xib文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!