我对Swift非常陌生。我正在尝试在按下UILabel
时使UIButton
产生文本。我在StackOverflow上找到了this的示例,并且一直在尝试遵循,但是由于打印不正确,只是将屏幕更改为空白。为什么会这样?
期望的结果是让UILabel
说出枚举的字符串。在这种情况下,控制台会正确打印我要查找的内容,但不会打印UILabel
。
如果这是重复的或者我错过了什么,请指出正确的方向,我将删除此帖子。
这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textDump: UILabel!
@IBOutlet weak var textButton: UIButton!
var messageText = "Initial View"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.backgroundColor = .black
textDump.textColor = .white
showText()
}
func showText(){
textDump.text! = messageText
textDump.textColor = .white
print(textDump.text!)
}
@IBAction func buttonPress(_ sender: Any){
var newText = ""
for (n, c) in "Swift".enumerated(){
newText = newText + "\n \(n): '\(c)' \n"
}
messageText = newText
showText()
}
}