Changing text of Swift UILabel

后端 未结 4 1559
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 21:51

I am attempting to learn Apple\'s Swift. I was recently trying to build a GUI app, but I have a question:

How do I interact with GUI elements of my app? For instance

4条回答
  •  我在风中等你
    2020-12-30 22:12

    If you have something like this for an IBOutlet:

    @IBOutlet var someLabel: UILabel!
    

    then you could set the text property just like in your example:

    someLabel.text = "Whatever text"
    

    If you're having problems with this, perhaps you're not assigning the text property in the right place. If it's in a function that doesn't get called, that line won't execute, and the text property won't change. Try overriding the viewDidLoad function, and put the line in there, like this:

    override func viewDidLoad() {
        super.viewDidLoad()
        someLabel.text = "Whatever text"
    }
    

    Then, as soon as the view loads, you'll set the text property. If you're not sure if a line of code is executing or not, you can always put a breakpoint there, or add some output. Something like

    println("Checkpoint")
    

    inside a block of code you're unsure about could really help you see when and if it runs.

    Hope this helps.

提交回复
热议问题