自定义UIView上的警告

时间:2015-11-08 10:01:58

标签: ios swift uiview

我正在创建弹出式自定义UIView。我创建了custom.xib,custom.swift。在我的custom.xib中,我的所有者的对象引用自定义。我在custom.swift和loadNib中使用名称" custom"实现了init func。我从init func获得了无限调用,直到我在super.init(coder: aDecoder)收到此警告和断点。

  

警告:无法加载任何Objective-C类信息。这将   显着降低了可用类型信息的质量。

的ViewController

let customView: Custom = Custom(frame: CGRectMake(100,100,200,200))
self.view.addSubview(customView)

自定义

var view: Custom!
override init(frame: CGRect) {
    super.init(frame: frame)
    loadNib()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    loadNib()
}

private func loadNib() -> Custom {
    return NSBundle.mainBundle().loadNibNamed("Custom", owner:  self, options: nil)[0] as! Custom
}

2 个答案:

答案 0 :(得分:1)

在这种情况下,您的loadNib()方法毫无用处。它返回Custom视图的实例,但结果从未被init方法使用。您可以将loadNib()声明为类方法,并在init类中删除Custom

class Custom: UIView {
    class func loadNib() -> Custom {
        return NSBundle.mainBundle().loadNibNamed("Custom", owner:  self, options: nil)[0] as! Custom
    }
}

然后,您可以使用loadNib()方法直接在Custom中实例化ViewController视图,并以这种方式更改框架:

let customView = Custom.loadNib()
customView.frame = CGRectMake(100,100,200,200)
self.view.addSubview(customView)

答案 1 :(得分:0)

您收到此错误的原因是您的loadNib()init方法导致递归。 imnosov answer应该解决你的问题。

相关问题