在 While 循环中更新文本 Swift
Update Text in While Loop Swift
我的程序有一个 while 循环,它运行一些代码,生成各种文本语句。问题是 UILabel 只打印系列中的最后一行文本(我的理解是因为它迭代太快了)。如何让标签打印遇到的所有文本,就像在控制台输出中看到的那样?
我查看了这个 link,但该示例似乎与我的情况不符,并且不确定如何实施修复(如果这是正确的):
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 的所有其他 "timestamp" 事件。
您每次都在设置标签的文本,而不是附加到标签上。替换此行:
labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
有了这个:
labelText.text += "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
并替换此行:
labelText.text = "\(timeStamp) They are in the same place."
有了这个:
labelText.text += "\(timeStamp) They are in the same place."
请注意,唯一的区别是我将您的 +
运算符更改为 +=
运算符。
希望对您有所帮助!
如果您想在每次 if 条件为真时添加新的文本行,您需要追加文本并使用换行符,\n
while (playerDead != true) {
//...
if playerOnePosition != playerTwoPosition {
labelText.text =
(labelText.text ?? "" ) +
"\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)\n"
}
if playerOnePosition == playerTwoPosition {
labelText.text = (labelText.text ?? "" ) + "\(timeStamp) They are in the same place."
playerDead = true
}
}
同时让您的标签显示任意行数,将限制设置为 0
labelText.numberOfLines = 0
我的程序有一个 while 循环,它运行一些代码,生成各种文本语句。问题是 UILabel 只打印系列中的最后一行文本(我的理解是因为它迭代太快了)。如何让标签打印遇到的所有文本,就像在控制台输出中看到的那样?
我查看了这个 link,但该示例似乎与我的情况不符,并且不确定如何实施修复(如果这是正确的):
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 的所有其他 "timestamp" 事件。
您每次都在设置标签的文本,而不是附加到标签上。替换此行:
labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
有了这个:
labelText.text += "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
并替换此行:
labelText.text = "\(timeStamp) They are in the same place."
有了这个:
labelText.text += "\(timeStamp) They are in the same place."
请注意,唯一的区别是我将您的 +
运算符更改为 +=
运算符。
希望对您有所帮助!
如果您想在每次 if 条件为真时添加新的文本行,您需要追加文本并使用换行符,\n
while (playerDead != true) {
//...
if playerOnePosition != playerTwoPosition {
labelText.text =
(labelText.text ?? "" ) +
"\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)\n"
}
if playerOnePosition == playerTwoPosition {
labelText.text = (labelText.text ?? "" ) + "\(timeStamp) They are in the same place."
playerDead = true
}
}
同时让您的标签显示任意行数,将限制设置为 0
labelText.numberOfLines = 0