awakeAfterUsingCoder()导致EXC_BAD_ACCESS

时间:2015-09-30 23:54:07

标签: ios swift exception

我在InterfaceBuilder中设计了一个具有Swift支持类的UIView。我正在使用awakeAfterUsingCoder()替换实例化的nib,如下所示:

public override func awakeAfterUsingCoder(aDecoder: NSCoder) -> AnyObject? {
    if self.subviews.count == 1 && self.subviews[0].isKindOfClass(UILabel) {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "CustomView", bundle: bundle).instantiateWithOwner(nil, options: nil)
        return nib[0] as! CustomView
    }

    return self
}

但是,现在我不断在AppDelegate中遇到EXC_BAD_ACCESS崩溃。我打开了僵尸并且能够得到这个消息:

  

*** - [ModuleName.CustomView _referenceView]:发送到解除分配的实例0xed13600的消息

必须在框架中的某处定义

_referenceView,但是当我导航self在断点处停止时我没有看到它。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我的故事板中的视图被定义为UIButton(因为我的CustomView是一个UIButton子类)。我用一个通用的空UIView替换它,我将条件替换为:

if self.subviews.isEmpty {

一切正常。

相关问题