2

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()
        }

}
9
  • What color is the label?
    – Don
    Commented Mar 25, 2019 at 21:14
  • The label a black background with white text. The label appears correctly for the initial viewDidAppear method.
    – Lou
    Commented Mar 25, 2019 at 21:16
  • Ok. I saw you using white text and white text on a white background would not show. I don't see anything wrong with your code.
    – Don
    Commented Mar 25, 2019 at 21:19
  • 2
    Did you set number of lines to 0?
    – Don
    Commented Mar 25, 2019 at 21:25
  • 1
    Default is 1. Setting it to 0, means unlimited.
    – Don
    Commented Mar 25, 2019 at 21:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.