本文介绍了为什么Xcode没有添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 gm stepper,它对应于标签.我有 4 个不同的标签和一个用于添加到 values 的附加标签.对应于 GM Steppers 的标签工作良好,但我在标签的附加值方面遇到了困难.

I use gm stepper in my app and , it corresponding to labels. I have 4 different labels and one additional label for addition to values . The labels which corresponding to GM Steppers working well but ı stumble in addition values of labels.

class ViewController: UIViewController {

@IBAction func gmstp1(_ sender: GMStepper) {
    label1.text = String(sender.value*1.5)
    label6.text = String(sender.value)
}

@IBOutlet weak var label1: UILabel!


@IBAction func gmstp2(_ sender: GMStepper) {
    label2.text = String(sender.value*0.89)

}
@IBOutlet weak var label2: UILabel!


@IBAction func gmstp3(_ sender: GMStepper) {
    label3.text = String(sender.value*26)

}
@IBOutlet weak var label3: UILabel!


@IBAction func gmstp4(_ sender: GMStepper) {
    label4.text = String(sender.value*4)


}
@IBOutlet weak var label4: UILabel!

@IBOutlet weak var label5: UILabel!

我的问题;有没有不使用按钮的解决方案?你能处理吗?

My question about ; Is there any solution without using buttons? Could you handle that?

*Label5 用于摘要.

*Label5 using for summary.

推荐答案

只是因为与 GMStepper 不同,UILabel 没有 value 属性.因此,您必须在每个标签中获取 text 字符串,将其转换为 Double,然后将它们相加:

Simply because, unlike a GMStepper, a UILabel doesn't have a value property. So you'll have to get the text string in each label, convert it to a Double, and then add them up:

if let text1 = label1.text, let value1 = Double(text1),
    let text2 = label2.text, let value2 = Double(text2),
    let text3 = label3.text, let value3 = Double(text3),
    let text4 = label4.text, let value4 = Double(text4) {
    let sum = value1 + value2 + value3 + value4
    label5.text = String(sum)
}

这篇关于为什么Xcode没有添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:26