【发布时间】:2019-07-21 12:33:02
【问题描述】:
我的程序有一个 while 循环,它运行一些代码,在执行过程中生成各种文本语句。问题是 UILabel 只打印系列中的最后一行文本(我的理解是因为它迭代得太快了)。如何让标签打印遇到的所有文本,就像在控制台输出中看到的那样?
我查看了此链接,但该示例似乎与我的情况不匹配,并且不确定如何实施修复(如果这甚至是正确的):Update Label In While Loop Swift
class ViewController: UIViewController {
var locationArray = ["Place A", "Place B", "Place C", "Place D"]
var timeStamp = 0
var playerDead = false
@IBOutlet var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
} //end viewDidLoad
@IBAction func startGame(_ sender: Any) {
var playerOnePosition = locationArray.randomElement()!
var playerTwoPosition = locationArray.randomElement()!
while (playerDead != true) {
playerOnePosition = locationArray.randomElement()!
playerTwoPosition = locationArray.randomElement()!
timeStamp += 1
if playerOnePosition != playerTwoPosition {
labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
} //End first if statement
if playerOnePosition == playerTwoPosition {
labelText.text = "\(timeStamp) They are in the same place."
playerDead = true //Game ends here
} //End second if statement
} //End while loop
} //End function
} //End class
通常输出的示例是“13 它们在同一个地方”,但我希望 UIlabel 打印所有其他“时间戳”事件,直到 13。
【问题讨论】:
标签: swift while-loop label viewcontroller