标签显示两次 Swift
Label shown twice Swift
我的柜台申请有问题。启动计数器时一切正常,但标签显示初始值 0.0 加上新时间 += 0.2。在下面的照片中查看结果。有没有人有办法解决吗?谢谢!
import UIKit
class CounterViewController: UIViewController {
var time = 0.0
var timer = Timer()
@IBOutlet weak var timerLabel:UILabel!
@IBAction func startCounter(_ sender:UIButton){
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(CounterViewController.startAction), userInfo:nil, repeats: true)
}
@IBAction func pauseCounter(_ sender:UIButton){
timer.invalidate()
}
@IBAction func stopCounter(_ sender:UIButton){
timer.invalidate()
time = 0.0
timerLabel.text = "\(time)"
}
@objc func startAction(){
time += 0.2
timerLabel.text = "\(time)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
see label twice
在计时器声明中试试这个
var timer:Timer?
并在 viewDidLoad
中设置
timerLabel.text = "0.0"
同时检查你的 xib 或故事板是否添加了两次相同的标签
我的柜台申请有问题。启动计数器时一切正常,但标签显示初始值 0.0 加上新时间 += 0.2。在下面的照片中查看结果。有没有人有办法解决吗?谢谢!
import UIKit
class CounterViewController: UIViewController {
var time = 0.0
var timer = Timer()
@IBOutlet weak var timerLabel:UILabel!
@IBAction func startCounter(_ sender:UIButton){
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector:#selector(CounterViewController.startAction), userInfo:nil, repeats: true)
}
@IBAction func pauseCounter(_ sender:UIButton){
timer.invalidate()
}
@IBAction func stopCounter(_ sender:UIButton){
timer.invalidate()
time = 0.0
timerLabel.text = "\(time)"
}
@objc func startAction(){
time += 0.2
timerLabel.text = "\(time)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
see label twice
在计时器声明中试试这个
var timer:Timer?
并在 viewDidLoad
中设置timerLabel.text = "0.0"
同时检查你的 xib 或故事板是否添加了两次相同的标签