Swift 3 计数计时器无法在 UILabel 中显示更新的数字
Swift 3 Counting up timer failed to show the updated number in UILabel
我有下面的计数定时器,但是在我运行这个程序之后,UILable从0开始生成到()。但是柜台号码是正确的,因为我可以看到打印日志是正确的。
我可以知道代码出了什么问题吗?`
@IBOutlet weak var labelForBinaryCount: UILabel!
var timer = Timer()
var counter = 0
@IBAction func start() {
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(Resting.updateCounter), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
labelForBinaryCount.text = String(counter)
}
func updateCounter() {
labelForBinaryCount.text = String(describing: counter += 1)
print(counter)
}
@IBAction func pauseButton(sender: AnyObject) {
timer.invalidate()
}
@IBAction func clearButton(sender: AnyObject) {
timer.invalidate()
counter = 0
labelForBinaryCount.text = String(counter)
}`
试试这个:
func updateCounter() {
counter += 1
labelForBinaryCount.text = String(counter)
print(counter)
}
我想你明白了。 ;)
我有下面的计数定时器,但是在我运行这个程序之后,UILable从0开始生成到()。但是柜台号码是正确的,因为我可以看到打印日志是正确的。 我可以知道代码出了什么问题吗?`
@IBOutlet weak var labelForBinaryCount: UILabel!
var timer = Timer()
var counter = 0
@IBAction func start() {
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(Resting.updateCounter), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
labelForBinaryCount.text = String(counter)
}
func updateCounter() {
labelForBinaryCount.text = String(describing: counter += 1)
print(counter)
}
@IBAction func pauseButton(sender: AnyObject) {
timer.invalidate()
}
@IBAction func clearButton(sender: AnyObject) {
timer.invalidate()
counter = 0
labelForBinaryCount.text = String(counter)
}`
试试这个:
func updateCounter() {
counter += 1
labelForBinaryCount.text = String(counter)
print(counter)
}
我想你明白了。 ;)