increment/decrement 带有两个按钮的标签的值
increment/decrement the value of a label with two buttons
作为 swift 的新手意味着我在使用两个按钮来递增 label.I 的值时遇到问题,因为递增按钮有效但递减按钮不起作用。我想存储在标签内的值减少了,但标签没有更新。这是代码,谢谢
PS:所有这些 isEnabled = true 或 false 只是为了禁用按钮能够创建从 0 到 5 的范围
class ViewController: UIViewController {
var incDec = 0;
@IBOutlet weak var countLbl: UILabel!
@IBOutlet weak var decBtn: UIButton!
@IBOutlet weak var incBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapDec(_ sender: Any) {
if(incDec >= -1){
incDec -= 1;
self.countLbl.text = "\(incDec)"
decBtn.isEnabled = true
}
else{
self.countLbl.text = "\(incDec)"
decBtn.isEnabled = false
incBtn.isEnabled = true
}
}
@IBAction func tapInc(_ sender: Any) {
if(incDecVal < 5){
incDec += 1;
self.countLbl.text = "\(incDec)"
incBtn.isEnabled = true
}
else{
self.countLbl.text = "\(incDec)"
incBtn.isEnabled = false
decBtn.isEnabled = true
}
}
}
您必须修改启用和禁用按钮的逻辑。这是代码。
class ViewController: UIViewController {
var incDecVal = 0;
@IBOutlet weak var countLbl: UILabel!
@IBOutlet weak var decBtn: UIButton!
@IBOutlet weak var incBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
countLbl.text = "\(incDecVal)"
}
@IBAction func tapDec(_ sender: Any) {
if(incDecVal > 0){
incDecVal = incDecVal - 1;
}
decBtn.isEnabled = incDecVal == 0 ? false : true
incBtn.isEnabled = true
self.countLbl.text = "\(incDecVal)"
}
@IBAction func tapInc(_ sender: Any) {
if(incDecVal <= 5) {
incDecVal = incDecVal + 1;
}
incBtn.isEnabled = incDecVal == 5 ? false : true
decBtn.isEnabled = true
self.countLbl.text = "\(incDecVal)"
}
}
作为 swift 的新手意味着我在使用两个按钮来递增 label.I 的值时遇到问题,因为递增按钮有效但递减按钮不起作用。我想存储在标签内的值减少了,但标签没有更新。这是代码,谢谢 PS:所有这些 isEnabled = true 或 false 只是为了禁用按钮能够创建从 0 到 5 的范围
class ViewController: UIViewController {
var incDec = 0;
@IBOutlet weak var countLbl: UILabel!
@IBOutlet weak var decBtn: UIButton!
@IBOutlet weak var incBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapDec(_ sender: Any) {
if(incDec >= -1){
incDec -= 1;
self.countLbl.text = "\(incDec)"
decBtn.isEnabled = true
}
else{
self.countLbl.text = "\(incDec)"
decBtn.isEnabled = false
incBtn.isEnabled = true
}
}
@IBAction func tapInc(_ sender: Any) {
if(incDecVal < 5){
incDec += 1;
self.countLbl.text = "\(incDec)"
incBtn.isEnabled = true
}
else{
self.countLbl.text = "\(incDec)"
incBtn.isEnabled = false
decBtn.isEnabled = true
}
}
}
您必须修改启用和禁用按钮的逻辑。这是代码。
class ViewController: UIViewController {
var incDecVal = 0;
@IBOutlet weak var countLbl: UILabel!
@IBOutlet weak var decBtn: UIButton!
@IBOutlet weak var incBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
countLbl.text = "\(incDecVal)"
}
@IBAction func tapDec(_ sender: Any) {
if(incDecVal > 0){
incDecVal = incDecVal - 1;
}
decBtn.isEnabled = incDecVal == 0 ? false : true
incBtn.isEnabled = true
self.countLbl.text = "\(incDecVal)"
}
@IBAction func tapInc(_ sender: Any) {
if(incDecVal <= 5) {
incDecVal = incDecVal + 1;
}
incBtn.isEnabled = incDecVal == 5 ? false : true
decBtn.isEnabled = true
self.countLbl.text = "\(incDecVal)"
}
}