ios – Swift正确的方式来加载xib文件

前端之家收集整理的这篇文章主要介绍了ios – Swift正确的方式来加载xib文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 swift项目中加载xib文件有一些奇怪的问题.这是非常令人沮丧的,因为我已经知道如何在Obj-C.但是因为迅捷是迅速的,所以你不能像你这样做.. .. /

所以我创建了IconTextFiled.xib和IconTextField.swift. (扩展UITextField)在xib我填充字段类在Idenity检查器和在故事板我做同样的一些textFields.所以我们很好,只需要从xib加载到init方法?没有.

在objc我会这样做

  1. - (instancetype)initWithCoder:(NSCoder *)coder
  2. {
  3. self = [super initWithCoder:coder];
  4. if (self) {
  5. NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IconTextField" owner:self options:nil];
  6. self = [nib objectAtIndex:0];
  7. }
  8. return self;
  9. }

所以我想如果我翻译成swift它会是好的.

  1. required init(coder aDecoder: NSCoder) {
  2. super.init(coder: aDecoder)
  3. let nib:NSArray = NSBundle.mainBundle().loadNibNamed("IconTextField",owner: self,options: nil)
  4. self = nib.objectAtIndex(0)
  5. }

但是这不行.我不知道为什么,但它尝试创造更多的对象和崩溃

最后我发现扩展

  1. extension IconTextField {
  2. class func loadFromNibNamed(nibNamed: String,bundle : NSBundle? = nil) -> IconTextField? {
  3. return UINib(
  4. nibName: nibNamed,bundle: bundle
  5. ).instantiateWithOwner(nil,options: nil)[0] as? IconTextField
  6. }
  7. }

所以在ViewController看起来像

  1. @IBOutlet var password: IconTextField!
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. password = IconTextField.loadFromNibNamed("IconTextField")
  5. }

再次失败.你能告诉我如何加载和使用xib文件

UPDATE

好吧,跟随丹尼尔之后

我当前的代码

  1. class IconTextField: UITextField {
  2. @IBOutlet var icon: UIImageView!
  3. @IBOutlet weak var view: UIView!
  4.  
  5. required init(coder aDecoder: NSCoder) {
  6. super.init(coder: aDecoder)
  7. NSLog("initWithCoder \(self)")
  8. NSBundle.mainBundle().loadNibNamed("IconTextField",options: nil)
  9. self.addSubview(view)
  10. }
  11. }

那两个var连接到这些视图

Consoleo输出,大大多了,以EXC_BAD_ACCESS结尾

  1. 2014-10-24 10:09:09.984 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7bfb0;>
  2. 2014-10-24 10:09:09.984 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7ddf0;>
  3. 2014-10-24 10:09:09.985 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be7fa20;>
  4. 2014-10-24 10:09:09.985 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be814f0;>
  5. 2014-10-24 10:09:09.986 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7be830c0;>
  6. 2014-10-24 10:09:10.083 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d183270;>
  7. 2014-10-24 10:09:10.084 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d187cd0;>
  8. 2014-10-24 10:09:10.084 testproject[20337:3757479] initWithCoder <stensgroup.IconTextField: 0x7d189960;>

应该只有两个initWithCoder.它接收到func loadNibNamed调用initWithCoder

解决方法

这对我有用:
  1. class IconTextField: UITextField {
  2. @IBOutlet weak var view: UIView!
  3. @IBOutlet weak var test: UIButton!
  4.  
  5. required init(coder: NSCoder) {
  6. super.init(coder: coder)
  7. NSBundle.mainBundle().loadNibNamed("IconTextField",options: nil)
  8. self.addSubview(view)
  9. assert(test != nil,"the button is conected just like it's supposed to be")
  10. }
  11. }

一旦loadNibNamed:owner:options:被称为视图和测试按钮按预期连接到插座.添加笔尖的视图自己的子视图层次结构使得笔尖的内容可见.

猜你在找的iOS相关文章