swift - 将 4 个 UI 标签(与 4 个 slider 链接)值添加在一起

标签 swift user-interface uilabel uislider

有问题的标签只能是整数值 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.
    }


}

View the code corresponds with

最佳答案

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/

相关文章:

ios - IBOutlets 在 iOS 8 设备上为零,但在 iOS 9 (Swift) 上工作正常

ios - 无法在我的 iPhone 上通过 Xcode 构建 swift 项目

java - 在 JSpinner 中禁用数字分组

iphone - 自定义 uitableviewcell 的问题

ios - 如何在自定义 CNContactViewController 中异步更新图像

c++ - Windows 中的非主 GUI 线程究竟何时退出?

user-interface - bazaar 的 GUI,相当于 gitk

ios - 将 View 动态移动到自动布局中的新位置

objective-c - 如何在 UILabel iOS 中显示表情符号

ios - 如何将 SKProduct 价格格式化为 Swift 货币字符串?