How to Update Uibutton Title/Text Programmatically

is it possible to update UIButton title/text programmatically?

Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.


Edit: While it's not the case here, something like this can also happen if you set an attributed title to the button, then you try to change the title and not the attributed title.

Changing text of UIButton programmatically swift

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.

How to change a button's title programmatically]

Well....This is awkward but you have to set your UIButton title to Plain in storyboard to have it changed

ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)

+

Plain UIButton Title

=

Happy Camper

How to change UIButton title text dynamically

Your button is dynamically attached, you must create a reference to it.

You can use a counter, like this:

-(void) aMethod{

if(ctr < arrayLength){
ctr++;
[buttonReference setText:@"Show More"];
} else {
ctr--;
[buttonReference setText:@"Return"];
}
.....
}

Programmatically change button text]

it should be like this :

For Objective C :

[_button setTitle:@"example text" forState:UIControlStateNormal];

For Swift :

_button.setTitle("example Text", forState: UIControlState.Normal)

Swift 3 changing the title of a UIButton

Your button needs to be an IBOutlet rather than an IBAction. When you ctrl-drag from Interface Builder to your code, make sure you select the 'Outlet' option as shown below:

IB - Code illustration

Then you can change the button title like this:

else {
startButton.setTitle("Button Title", for: .normal)
stopFuncs = true
}


Related Topics



Leave a reply



Submit