gpt4 book ai didi

ios - 让一个按钮减去我按下的下一个按钮的值。 swift

转载 作者:行者123 更新时间:2023-11-28 15:25:25 25 4
gpt4 key购买 nike

我的计数应用程序中有七个标记为 1-7 的按钮。计数从 0 开始,然后当我按一个数字时,它会加到 0。所以如果我按 6,计数为 6。然后我按 3,计数现在为 9。现在我想包括一个减法按钮,这样我就可以通过点击减法按钮来下降,然后选择我选择的值。因此,如果我点击减法按钮,然后点击数字 2,计数将回到 7。

    import UIKit

class ViewController: UIViewController {

@IBOutlet weak var Score: UILabel!

var number = 0

@IBAction func oneButton(_ sender: UIButton) {

var number1 = number + 1
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func twoButton(_ sender: UIButton) {

var number1 = number + 2
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func threeButton(_ sender: UIButton) {

var number1 = number + 3
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func fourButton(_ sender: UIButton) {

var number1 = number + 4
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func fiveButton(_ sender: UIButton) {

var number1 = number + 5
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func sixButton(_ sender: UIButton) {

var number1 = number + 6
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func sevenButton(_ sender: UIButton) {

var number1 = number + 7
number = number1
var realNumber = String(number)
Score.text = realNumber

}


@IBAction func foulButton(_ sender: UIButton) {



}

}

我不确定应该在 foulButton 函数中放入什么代码。提前谢谢你。

最佳答案

我首先为每个按钮提供“标签”及其数值,然后您可以对所有按钮使用一个函数。那么模拟一个减法就很容易了。

var subtraction = false

@IBAction func buttonAction(_ sender: UIButton) {

var change = sender.tag
if subtraction {
change = change * (-1)
subtraction = false
}

number = number + change
Score.text = String(number)
}

@IBAction func foulButton(_ sender: UIButton) {
subtraction = !subtraction
}

关于ios - 让一个按钮减去我按下的下一个按钮的值。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com