I'm new to Swift and I was making a basic app that changes the text of a lable with the text in an inputbox and I wanted to add RGB sliders to change the color of the lable that will be changed but I got an errors soon as I try to move a slider.
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
@IBOutlet weak var OutputLabel: UILabel!
@IBOutlet weak var InputField: UITextField!
@IBOutlet weak var RedSlider: UISlider!
@IBOutlet weak var BlueSlider: UISlider!
@IBOutlet weak var GreenSlider: UISlider!
@IBOutlet weak var RedLabel: UILabel!
@IBOutlet weak var GreenLabel: UILabel!
@IBOutlet weak var BlueLabel: UILabel!
var RedColor : Float = 0
var GreenColor : Float = 0
var BlueColor : Float = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ChangeBtn(_ sender: Any) {
OutputLabel.text = InputField.text
}
@IBAction func RedSliderAction(_ sender: UISlider) {
changeColor()
}
@IBAction func GreenSliderAction(_ sender: UISlider) {
changeColor()
}
@IBAction func BlueSliderAction(_ sender: UISlider) {
changeColor()
}
func changeOutputLableColor() {
OutputLabel.textColor = UIColor(red: CGFloat(RedColor), green: CGFloat(GreenColor), blue: CGFloat(BlueColor), alpha: 1.0)
changeLabelNumbers()
}
func changeColor() {
RedColor = RedSlider.value
GreenColor = GreenSlider.value
BlueColor = BlueSlider.value
changeOutputLableColor()
}
func changeLabelNumbers() {
let RoundedRed = String(format: "%0.0f", (RedColor * 255))
let RoundedGreen = String(format: "%0.0f", (GreenColor * 255))
let RoundedBlue = String(format: "%0.0f", (BlueColor * 255))
RedLabel.text = "\(RoundedRed)"
GreenLabel.text = "\(RoundedGreen)"
BlueLabel.text = "\(RoundedBlue)"
}
can someone help me figure it out?
You might have not connected the @IBOutlets RedSlider,BlueSlider and/or GreenSlider to the UISlider
on the storyboard. That'll result in the error you are receiving.
Alternatively you might have not connected the @IBOutlets RedLabel, GreenLabel and/or BlueLabel. Just look for the line which your codes crash at and it'll hint you which one is not connected.