I am very new to Swift. I'm trying make a UILabel
produce text when a UIButton
is pressed. I found this example on StackOverflow and have been trying to follow along, but instead of printing correctly it just changes the screen to blank. Why is this happening?
The desired result would be to have the UILabel
say the enumerated strings. The console prints correctly what I am looking for in this case, but not the UILabel
.
If this is a duplicate or I've missed something, kindly point me in the right direction and I will delete this post.
This is my code:
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()
}
}