我是 Swift 的新手,我正在制作一个基本的应用程序,它使用输入框中的文本更改标签的文本,我想添加 RGB 滑块来更改将要更改的标签的颜色,但当我尝试移动滑块时,我很快就遇到了错误。
致命错误:解开可选值时意外发现 nil (分行(
@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)"
}
有人可以帮我弄清楚吗?
您可能尚未将@IBOutlets RedSlider、BlueSlider 和/或 GreenSlider 连接到情节提要上的UISlider
。这将导致您收到的错误。
或者,您可能尚未连接@IBOutlets红标、绿标和/或蓝标。只需查找代码崩溃的行,它就会提示您哪一个未连接。