if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)
timerRunning = true
}
while timerLabel.text != "0"{
gameViewStillRunning = false
}
if gameViewStillRunning == false{
self.performSegueWithIdentifier("segue", sender: nil)
}
这段代码的目的是显示一个向下计数的标签,然后当它达到0时,场景应切换到另一个ViewController。这段代码没有任何错误,但是当我运行它时,该程序除了加载屏幕之外没有任何其他信息。有什么建议?
看来您正在while
主线程(也是负责绘制UI的线程)上运行循环。只要该线程停留在while循环中,就无法继续运行循环,也没有机会更新显示或响应用户交互。
另外,如果您查看NSTimer
文档,您可能会注意到它scheduledTimerWithTimeInterval
指出Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
计时器正在counting
主线程的运行循环中调度对函数的调用,但是由于该线程一直在您的内部旋转while
,它将永远没有机会执行,因此它没有机会更新timerLabel.text
。
while
更好的解决方案是让run循环继续进行,并在计时器调用您的counting
函数时做出反应,而不是尝试在轮询某些条件时用循环来阻止执行。无论事件何时发生或如何发生,让您的应用程序对事件做出反应(例如剩余时间更改),而不是尝试控制确切的执行顺序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句