有问题的标签只能是整数值 0-8,所以我不知道为什么编译器拒绝我将四个标签的值加在一起的每一次尝试。这一切都在 Xcode 版本 9.0.1 中。我的目标是当单击“提交”按钮时,让 MathValue(稍后我将小写名称)作为所有 4 个 slider 的值。
//
// ViewController.swift
// InnovationSS
//
// Created by AJ Admin on 8/16/18.
// Copyright © 2018 AJ Admin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//The slider that the students will use to input their grades into the app
@IBOutlet weak var MathCriASlider: UISlider!
@IBOutlet weak var MathCriAValue: UILabel!
//label is now equal to value of slider
@IBAction func MathCriAChanged(_ sender: UISlider) {
let currentValue = Int(sender.value)
MathCriAValue.text = "\(currentValue)"
}
@IBOutlet weak var MathCriBSlider: UISlider!
@IBOutlet weak var MathCriBValue: UILabel!
@IBAction func MathCriBChanged(_ sender: UISlider) {
let currentValue = Int(sender.value)
MathCriBValue.text = "\(currentValue)"
}
@IBOutlet weak var MathCriCSlider: UISlider!
@IBOutlet weak var MathCriCValue: UILabel!
@IBAction func MathCriCChanged(_ sender: UISlider) {
let currentValue = Int(sender.value)
MathCriCValue.text = "\(currentValue)"
}
@IBOutlet weak var MathCriDSlider: UISlider!
@IBOutlet weak var MathCriDValue: UILabel!
@IBAction func MathCriDChanged(_ sender: UISlider) {
let currentValue = Int(sender.value)
MathCriDValue.text = "\(currentValue)"
}
@IBOutlet weak var MathValue: UILabel!
var aValue: Int = 0 {
didSet {
MathCriAValue.text = "\(aValue)"
}
}
var bValue: Int = 0 {
didSet {
MathCriBValue.text = "\(bValue)"
}
}
var cValue: Int = 0 {
didSet {
MathCriCValue.text = "\(cValue)"
}
}
var dValue: Int = 0 {
didSet {
MathCriDValue.text = "\(dValue)"
}
}
@IBAction func mathValueChanged(_ sender: UISlider) {
aValue = Int(sender.value)
}
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.
}
}
最佳答案
All the outlets are optionals
`您需要解开该值,然后只有您可以使用它。
尝试以下操作
@IBAction func mathValueChanged(_ sender: UIButton){
guard let aValue = Int(MathCriAValue.text) , let bValue = Int(MathCriBValue.text) , let cValue = Int(MathCriCValue.text), let dValue = Int(MathCriDValue.text) else { return }
let Mathvalue = aValue + bValue + cValue + dValue
}
关于swift - 将 4 个 UI 标签(与 4 个 slider 链接)值添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956065/