Changing text of UIButton programmatically swift
Asked Answered
M

13

279

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?

Masaryk answered 12/10, 2014 at 14:42 Comment(0)
F
717

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.

Fridafriday answered 12/10, 2014 at 14:48 Comment(5)
You can omit UIControlState. e.g. forState: .NormalWileywilfong
Swift3 is now .normal note the lower caseOppidan
Swift3 change forState to forStimulative
Swift3: self.btnBuy.setTitle("Buy", for: UIControlState.normal)Somatoplasm
for swift 4 also button.setTitle("Button Title",for: .normal) working!, thanksSardanapalus
C
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.

Carcinoma answered 8/6, 2015 at 1:41 Comment(6)
Yep, I was about to google exactly this until I saw your answer. Thanks! +1Junna
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.Carcinoma
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.Loralorain
Neither Bendix answer nor his comment are correct. They are plain wrong.Fiesta
The question doesn't mention interface builder so wrong to assume that is the case.Putumayo
It also will throw an error warning if you are not updating it in the main thread. That appears to be new.Intelligent
E
20

Swift 5.0

// Standard State
myButton.setTitle("Title", for: .normal)
Edward answered 24/3, 2017 at 13:27 Comment(0)
S
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)
    }
Southing answered 27/3, 2018 at 5:13 Comment(2)
I had to use UIControl.State. It said UIControlState was deprecatedPoisson
This is unnecessary. If you only set the title for the .normal state, then that title will be used for all states.Quadripartite
G
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)
Grammatical answered 23/12, 2016 at 12:30 Comment(0)
V
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...

Volauvent answered 19/10, 2018 at 8:36 Comment(0)
N
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

Nolan answered 21/1, 2017 at 23:35 Comment(0)
M
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)
}
Mantoman answered 25/4, 2019 at 9:59 Comment(0)
P
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)
Pavlish answered 13/12, 2021 at 1:45 Comment(1)
This is already covered in several much older answers.Quadripartite
M
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)
Misbehave answered 12/5, 2017 at 16:29 Comment(0)
O
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)
Ollayos answered 29/10, 2018 at 19:52 Comment(0)
F
-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

Fifi answered 24/8, 2023 at 23:51 Comment(0)
B
-1

Swift 5.9

To change the title of a button in Swift, you can access the title property of the button and set it to the desired text. Here's an example:

override func viewDidLoad() { super.viewDidLoad()

    // Set the initial title of the button
    myButton.setTitle("Initial Title", for: .normal)
}

and in IBAction // Change the title of the button when a certain action occurs myButton.setTitle("New Title", for: .normal)

In this example, we have a button called myButton. In viewDidLoad(), we set the initial title of the button using setTitle(_:for:) with the .normal control state. Then, in the changeButtonTitle function (which you can associate with a user action, like a button tap), we change the title of the button to "New Title." You can modify the text as needed to change the button's title to whatever you want in response to different events in your app.

Brogle answered 29/9, 2023 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.