iOS Swift 1 秒后更新文本标签
iOS Swift Update text label after 1 second
1 秒后尝试更新标签。我正在使用休眠功能,但应用程序正在加载并且不会动态更新文本字段。
代码是:
override func viewDidAppear(animated: Bool) {
beginCountdown()
}
func beginCountdown() {
for var i = 5; i >= 0; i-- {
println("Time until launch \(i)")
var county:String = "\(i)"
countdownLabel.text = county
sleep(1)
}
}
插座是正确的,我知道我漏掉了什么。谢谢
您不应使用 sleep()
函数,因为这会暂停主线程并导致您的应用变得无响应。 NSTimer
是实现此目的的一种方法。它将在未来的指定时间派发一个函数。
例如-
var countdown=0
var myTimer: NSTimer? = nil
override func viewDidAppear(animated: Bool) {
countdown=5
myTimer = NSTimer(timeInterval: 1.0, target: self, selector:"countDownTick", userInfo: nil, repeats: true)
countdownLabel.text = "\(countdown)"
}
func countDownTick() {
countdown--
if (countdown == 0) {
myTimer!.invalidate()
myTimer=nil
}
countdownLabel.text = "\(countdown)"
}
你真的 不应该 使用 sleep
因为它会阻塞主线程并因此冻结 UI,这意味着你永远不会看到你的标签已更新(更糟糕的是)。
您可以使用 NSTimer
来实现您想要做的事情。
var timer: NSTimer!
var countdown: Int = 0
override func viewDidAppear(animated: Bool) {
self.countdown = 5
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateCountdown", userInfo: nil, repeats: true)
}
func updateCountdown() {
println("Time until launch \(self.countdown)")
countdownLabel.text = "\(self.countdown)"
self.countdown--
if self.countdown == 0 {
self.timer.invalidate()
self.timer = nil
}
}
在Swift3.0
var countdown=0
var myTimer: Timer? = nil
override func viewDidAppear(_ animated: Bool) {
countdown=5
myTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(Dashboard.countDownTick), userInfo: nil, repeats: true)
lbl_CustomerName.text = "\(countdown)"
}
func countDownTick() {
countdown = countdown - 1
//For infinite time
if (countdown == 0) {
countdown = 5
//till countdown value
/*myTimer!.invalidate()
myTimer=nil*/
}
lbl_CustomerName.text = "\(countdown)"
}
1 秒后尝试更新标签。我正在使用休眠功能,但应用程序正在加载并且不会动态更新文本字段。
代码是:
override func viewDidAppear(animated: Bool) {
beginCountdown()
}
func beginCountdown() {
for var i = 5; i >= 0; i-- {
println("Time until launch \(i)")
var county:String = "\(i)"
countdownLabel.text = county
sleep(1)
}
}
插座是正确的,我知道我漏掉了什么。谢谢
您不应使用 sleep()
函数,因为这会暂停主线程并导致您的应用变得无响应。 NSTimer
是实现此目的的一种方法。它将在未来的指定时间派发一个函数。
例如-
var countdown=0
var myTimer: NSTimer? = nil
override func viewDidAppear(animated: Bool) {
countdown=5
myTimer = NSTimer(timeInterval: 1.0, target: self, selector:"countDownTick", userInfo: nil, repeats: true)
countdownLabel.text = "\(countdown)"
}
func countDownTick() {
countdown--
if (countdown == 0) {
myTimer!.invalidate()
myTimer=nil
}
countdownLabel.text = "\(countdown)"
}
你真的 不应该 使用 sleep
因为它会阻塞主线程并因此冻结 UI,这意味着你永远不会看到你的标签已更新(更糟糕的是)。
您可以使用 NSTimer
来实现您想要做的事情。
var timer: NSTimer!
var countdown: Int = 0
override func viewDidAppear(animated: Bool) {
self.countdown = 5
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateCountdown", userInfo: nil, repeats: true)
}
func updateCountdown() {
println("Time until launch \(self.countdown)")
countdownLabel.text = "\(self.countdown)"
self.countdown--
if self.countdown == 0 {
self.timer.invalidate()
self.timer = nil
}
}
在Swift3.0
var countdown=0
var myTimer: Timer? = nil
override func viewDidAppear(_ animated: Bool) {
countdown=5
myTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(Dashboard.countDownTick), userInfo: nil, repeats: true)
lbl_CustomerName.text = "\(countdown)"
}
func countDownTick() {
countdown = countdown - 1
//For infinite time
if (countdown == 0) {
countdown = 5
//till countdown value
/*myTimer!.invalidate()
myTimer=nil*/
}
lbl_CustomerName.text = "\(countdown)"
}