简体   繁体   English

使用Swift在Xcode 6中使用Xib在自定义uiview中获取SIGABRT

[英]Getting SIGABRT in custom uiview using xib in Xcode 6 using Swift

I have created a custom view with two labels by following some tutorials online. 通过在线阅读一些教程,我创建了带有两个标签的自定义视图。 I have connected the custom xib to the swift class file with outlets for the two labels. 我已将自定义xib连接到带有两个标签出口的swift class文件。 When I initialise the view from the view controller where I need to present the view, I am getting a SIGABRT error on the line 当我从需要显示视图的视图控制器初始化视图时,在行上收到SIGABRT错误

let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

The code for the custom view class is as follows: 自定义视图类的代码如下:

import UIKit

@IBDesignable class LevelButton: UIView {

    @IBOutlet weak var levelLabel: UILabel!
    @IBOutlet weak var score: UILabel!
    var view:UIView!
    var levelLabelText:String?
    {
            get
            {
                return levelLabel.text
        }
        set(levelLabelText)
        {
            levelLabel.text = levelLabelText
        }
    }

    var scoreText:String?
        {
        get{
            return score.text
        }
        set(scoreText)
        {
            score.text = scoreText
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    required init(coder aDecoder:NSCoder)
    {
        super.init(coder: aDecoder)
        setup()
    }

    func setup()
    {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
        addSubview(view)
    }
    func loadViewFromNib() -> UIView
    {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "LevelButton", bundle: bundle)
        let view = nib.instantiateWithOwner(nil, options: nil)[0] as! UIView
        return view
    }

}

In the main storyboard, I added a view and set its custom class to the class with the above code, but it does not show anything and gives a SIGABRT error. 在主故事板上,我添加了一个视图,并使用上述代码将其自定义类设置为该类,但该类未显示任何内容,并给出了SIGABRT错误。

I am unable to figure out a solution. 我无法解决。 Please suggest (Swift only). 请提出建议(仅限Swift)。

This may be a solution for your question: 这可能是您的问题的解决方案:

Change this function: 更改此功能:

func setup()
{
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
    addSubview(view)
}

to: 至:

func setup() {

    if self.subviews.count == 0 {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
        addSubview(view)
    }

}

Source from: http://blog.boxuanzhang.me/custom-reusable-uiview-with-xib/ 来源: http//blog.boxuanzhang.me/custom-reusable-uiview-with-xib/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM