gpt4 book ai didi

ios - 在 SWIFT 4 中将文本字段限制为仅正整数

转载 作者:行者123 更新时间:2023-11-29 05:14:13 26 4
gpt4 key购买 nike

我有一个文本字段,可以使用包含整数的按钮增加金额,我不需要负数,它将从零开始。我怎样才能实现这个目标?

 @IBAction func beyazDOWN(_ sender: UIButton) {

calculateBUTTON.setTitle("=", for: .normal)

UIButton.animate(withDuration: 0.05, animations: {
sender.transform = CGAffineTransform(scaleX: 0.700, y: 0.700)
}, completion: { finish in
UIButton.animate(withDuration: 0.1, animations:
{ sender.transform = CGAffineTransform.identity })
})

num3 = Int(field3.text!)!;
self.field3.text = String(num3 - 1);


}

最佳答案

var amount:Int{
willSet{
if newValue < 0{
textfield.text = "0"
}
}
}

其中 amount 是您将在文本字段上显示的值就像textfield.text = amount

根据您的代码:

var num3:Int{
willSet{
if newValue < 1{
beyazDOWN.isEnabled = false
}
else{
beyazDOWN.isEnabled = true
}
}
}

或者,您可以执行以下操作:

@IBAction func beyazDOWN(_ sender: UIButton) {

num3 = Int(field3.text!)!;
if num3 <= 1{
return
}


calculateBUTTON.setTitle("=", for: .normal)

UIButton.animate(withDuration: 0.05, animations: {
sender.transform = CGAffineTransform(scaleX: 0.700, y: 0.700)
}, completion: { finish in
UIButton.animate(withDuration: 0.1, animations:
{ sender.transform = CGAffineTransform.identity })
})

self.field3.text = String(num3 - 1);

}

关于ios - 在 SWIFT 4 中将文本字段限制为仅正整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352528/

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