274

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

currencySelector.text = "foobar"

Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change?

Cœur
  • 37,241
  • 25
  • 195
  • 267
rocket101
  • 7,369
  • 11
  • 45
  • 64

12 Answers12

711

In Swift 3, 4, 5:

button.setTitle("Button Title", for: .normal)

Otherwise:

button.setTitle("Button Title", forState: UIControlState.Normal)

Also an @IBOutlet has to declared for the button.

Majster
  • 3,611
  • 5
  • 38
  • 60
Gal Marom
  • 8,499
  • 1
  • 17
  • 19
77

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.

shim
  • 9,289
  • 12
  • 69
  • 108
Bendrix
  • 1,033
  • 7
  • 10
  • 1
    Yep, I was about to google exactly this until I saw your answer. Thanks! +1 – Evernoob Jun 18 '15 at 16:03
  • 2
    It is a pattern in IOS that took me a while to discover. There are attributes of UI items that you have no access to unless an IBOutlet is created. If ever you are trying to change an attribute of a UI and can't access it, make sure you have both the IBAction that is running some code, and the IBOutlet that provides access to the attributes. – Bendrix Jun 18 '15 at 18:32
  • 8
    This answer makes no sense. The `sender` of the action will be the button. You can apply anything you wish to the `sender`. You don't need an outlet to do this. – rmaddy Apr 18 '16 at 18:24
  • 2
    Neither Bendix answer nor his comment are correct. They are plain wrong. – vikingosegundo Apr 18 '16 at 18:48
  • 1
    The question doesn't mention interface builder so wrong to assume that is the case. – Edward Jun 26 '17 at 19:41
  • It also will throw an error warning if you are not updating it in the main thread. That appears to be new. – Douglas W. Palme Jun 03 '21 at 19:05
19

Swift 5.0

// Standard State
myButton.setTitle("Title", for: .normal)
Tyler Rutt
  • 567
  • 2
  • 8
  • 24
17

Swift 5:

    let controlStates: Array<UIControl.State> = [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved]
    for controlState in controlStates {
        button.setTitle(NSLocalizedString("Title", comment: ""), for: controlState)
    }
Dmitry
  • 14,306
  • 23
  • 105
  • 189
14

Swift 3:

Set button title:

//for normal state:
my_btn.setTitle("Button Title", for: .normal)

// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
9

Changing title when attributed is a bit different :

I just ran into a problem : If you have an UIButton with an Attributed Title, you have to use :

my_btn.setAttributedTitle(NSAttributedString(string: my_title), for: my_state)

as, per Apple SetTitle Doc :

If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.

I had an attributed title and I tried to setTitle on it, with no effect...

Christian Navelot
  • 1,174
  • 9
  • 9
4

Swift 3

When you make the @IBAction:

@IBAction func btnAction(_ sender: UIButton) {
  sender.setTitle("string goes here", for: .normal)
}

This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton

gustavoanalytics
  • 584
  • 6
  • 18
3

swift 4.2 and above

using button's IBOutlet

btnOutlet.setTitle("New Title", for: .normal)

using button's IBAction

@IBAction func btnAction(_ sender: UIButton) {
  sender.setTitle("New Title", for: .normal)
}
midhun p
  • 1,987
  • 18
  • 24
3

As of 12/12/2021 - Swift version 5.5.1^ assuming you already have an IBOutlet linked to yourButton in a normal state.

yourButton.setTitle("Title of your button", for: .normal)
Man of Progress
  • 129
  • 2
  • 14
0

Swift 3

let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)
Giang
  • 3,553
  • 30
  • 28
0

To set a title for a button in Xcode using swift - 04: first create a method called setTitle with parameter title and UIController state like below ;

func setTitle(_ title : String?, for state : UIControl.State)   {

}

and recall this method in your button action method like ;

yourButtonName.setTitle("String", for: .state)
-1

Also, in addition to the code, make sure your Title is set to "Plain" not "Attributed" in the Xcode Inspector.

enter image description here

Dave Levy
  • 1,036
  • 13
  • 20