Search code examples
swiftmacosnstouchbar

Dynamic Label Title for NSTouchBar on Swift


I am new to swift and been learning how to do it on my own through tutorials. I was able to make the title of a button change when clicked on. I wanted to dynamically change the title of a label. It doesn't necessarily have to be clicked on like a button. This is a label for the NStouchbar.

The code below is how I managed to change the button label. I noticed that labels do not have the .title action so I am not sure how I would go about changing that.

@IBAction func buttonOneTapped(_ sender: Any) {
    print("Button One Tapped")
    buttonOne.title = "1 BTC = $" + dollar
}

Solution

  • To get the label of a button and edit it do it like this:

    button.titleLabel?.text
    

    And just as a suggestion, instead of using the + sign to concatenate a string and a float or double I would do it like this:

    "1 BTC = $\(dollar)"
    

    I forgot the name of this method :| but it's the more swifty way. :]