使用 Swift 在 Xcode 6 中使用 xib 在自定义 uiview 中获取 SIGABRT [英] Getting SIGABRT in custom uiview using xib in Xcode 6 using Swift
问题描述
我按照一些在线教程创建了一个带有两个标签的自定义视图.我已将自定义 xib 连接到带有两个标签的出口的 swift 类文件.当我从需要呈现视图的视图控制器初始化视图时,我在行上收到 SIGABRT
错误
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. 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
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
}
}
在主故事板中,我添加了一个视图并将其自定义类设置为具有上述代码的类,但它没有显示任何内容并给出了 SIGABRT 错误.
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.
我无法找到解决方案.请提出建议(仅限 Swift).
I am unable to figure out a solution. Please suggest (Swift only).
推荐答案
这可能是您问题的解决方案:
This may be a solution for your question:
更改此功能:
func setup()
{
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
addSubview(view)
}
到:
func setup() {
if self.subviews.count == 0 {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
addSubview(view)
}
}
来源:http://blog.boxuanzhang.me/custom-reusable-uiview-with-xib/
这篇关于使用 Swift 在 Xcode 6 中使用 xib 在自定义 uiview 中获取 SIGABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!