Changing text of UIButton programmatically swift

后端 未结 11 1512
天命终不由人
天命终不由人 2020-11-28 19:34

Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here\'s what I have:

currencySelector.text = \"foo         


        
11条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 20:20

    Just a clarification for those new to Swift and iOS programming. Below line of code:

    button.setTitle("myTitle", forState: UIControlState.Normal)
    

    only applies to IBOutlets, not IBActions.

    So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play to Pause based on a toggle variable, you need to also create an IBOutlet for that button.

    If you try to use button.setTitle against an IBAction you will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.

提交回复
热议问题