ChatGPT解决这个技术问题 Extra ChatGPT

Changing text of UIButton programmatically swift

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?


M
Majster

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.


You can omit UIControlState. e.g. forState: .Normal
Swift3 is now .normal note the lower case
Swift3 change forState to for
Swift3: self.btnBuy.setTitle("Buy", for: UIControlState.normal)
for swift 4 also button.setTitle("Button Title",for: .normal) working!, thanks
s
shim

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.


Yep, I was about to google exactly this until I saw your answer. Thanks! +1
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.
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.
Neither Bendix answer nor his comment are correct. They are plain wrong.
The question doesn't mention interface builder so wrong to assume that is the case.
T
Tyler Rutt

Swift 5.0

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

D
Dmitry

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

I had to use UIControl.State. It said UIControlState was deprecated
B
Bhavin Ramani

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)

C
Christian Navelot

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


g
gustavoanalytics

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


m
midhun p

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

y
yassine menssi

//for normal state:

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

M
Man of Progress

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)

G
Giang

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)

J
Jasani Hardik

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)