Changing text of UIButton programmatically swift

IosXcodeSwiftUibutton

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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.

Solution 3 - Ios

Swift 5.0

// Standard State
myButton.setTitle("Title", for: .normal)

Solution 4 - Ios

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)
    }

Solution 5 - Ios

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)

Solution 6 - Ios

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...

Solution 7 - Ios

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

Solution 8 - Ios

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)
}

Solution 9 - Ios

//for normal state:

btnSecurite.setTitle("TextHear", for: .normal)

Solution 10 - Ios

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)

Solution 11 - Ios

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)

Solution 12 - Ios

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrocket101View Question on Stackoverflow
Solution 1 - IosGal MaromView Answer on Stackoverflow
Solution 2 - IosBendrixView Answer on Stackoverflow
Solution 3 - IosTyler RuttView Answer on Stackoverflow
Solution 4 - IosDmitryView Answer on Stackoverflow
Solution 5 - IosBhavin RamaniView Answer on Stackoverflow
Solution 6 - IosChristian NavelotView Answer on Stackoverflow
Solution 7 - IosgustavoanalyticsView Answer on Stackoverflow
Solution 8 - Iosmidhun pView Answer on Stackoverflow
Solution 9 - Iosyassine menssiView Answer on Stackoverflow
Solution 10 - IosMan of ProgressView Answer on Stackoverflow
Solution 11 - IosGiangView Answer on Stackoverflow
Solution 12 - IosJasani HardikView Answer on Stackoverflow