I have the following code that creates and adds a slider and label to a view. As soon as I move the slider it gives a SIGABRT in the AppDelegate class and enters an infinite loop. I dont understand what am I doing wrong (if I only add the slider it works fine)
class test: UIView {
var value: Float
override func drawRect(rect: CGRect) {
self.addSubview(getSlider())
self.addSubview(getLabel())
}
override init(frame: CGRect) {
value = 0.9
super.init(frame:frame)
}
func getSlider()-> UISlider {
let slider: UISlider = UISlider(frame: CGRectMake(10,10,self.frame.width,30))
slider.addTarget(self, action:"change", forControlEvents:UIControlEvents.ValueChanged)
return slider
}
func getLabel()-> UILabel {
let label = UILabel(frame: CGRectMake(10,50,50,50))
label.text = "\(value)"
return label
}
func change(sender: UISlider) {
value = sender.value
}
-addSubview:
inside the-drawRect:
method is not the most brilliant idea; you should put those lines into a different place in the view lifecycle, for e.g. into-didMoveToSuperview
or-willMoveToSuperview:
(or their Swift equivalent, of course).