将4个UI标签(与4个滑块链接)值相加在一起

时间:2018-08-21 20:13:27

标签: swift user-interface uilabel uislider

有问题的标签只能是0到8的整数,所以我不知道为什么编译器会拒绝我每次尝试将四个标签的值加在一起的尝试。这一切在Xcode 9.0.1版中。我的目标是当单击“提交”按钮时,将MathValue(稍后将小写名​​称)作为所有4个滑块的值。

   //
//  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.
    }


}

View the code corresponds with

2 个答案:

答案 0 :(得分:0)

  

所有插座都是可选的

`您需要解开该值,然后只有您才能使用它。

尝试以下操作

@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 
}

答案 1 :(得分:0)

不要尝试将值存储在标签中。将4个Int变量添加到视图控制器aValuebValuecValuedValue

让滑块动作更改这些变量。向每个更改标签的didSet中添加一个didSet:

var aValue: Int {
  didSet {
     mathCriAValue.text = "\(aValue)"
  }
}

@IBAction func mathCriAChanged( sender: UISlider) {
   aValue = Int(sender.value)
}

然后,如果您需要这些值,则可以将它们作为Int值,而无需进行转换。

此外,变量名称应以小写字母开头,因此MathCriAValue应该为mathCriAValue