Change UIButton Text

So, I'm trying to update text in UIButton when I click it. I use the following line to change the text:

calibrationButton.titleLabel.text = @"Calibration"; 

I checked that the text is changing, but when I start the application and I press the button, it changes to β€œCalibration” for a split second, and then returns to its default value. Any ideas why this might happen? Is there any update function that I need to call?

+83
event-handling ios objective-c xcode uibutton
Jul 10 2018-12-12T00:
source share
6 answers

When placing its routines, UIButton will set its text value to titleLabel using its own header values, so you can configure up to four different lines for four states (normal, selected, selected, disabled).

Because of this function, setting the titleLabel text will not be saved directly and will be reset by the button when it displays its routines.

This is what you need to do to change the title text for the button state.

 [calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal]; 
+218
Jul 10 2018-12-12T00:
source share

To set the button text, use the following method:

 [calibrationButton setTitle: @"Calibration" forState: UIControlStateNormal]; 

See the UIButton class UIButton for more details ... http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

Or in Swift 3:

 calibrationButton.setTitle("Calibration", for: .normal) 
+28
Jul 10 2018-12-12T00:
source share

programmatically, you can set the name of the button as shown below:

 [myButton setTitle:@"buttonTitle" forState:UIControlStateNormal]; 

You can also set the button title property from the storyboard.

+4
Jul 11 '15 at 17:45
source share

Not a huge deal, and perhaps obvious, but there are several states available for buttons. If you specify "incorrect", you will not see the text change as you wish.

I noticed that my button did not show the text that I added using the methods given here. Check this link to make sure that you provide the UIControlState that you intend.

What is the difference between UIControlStateHighlighted and UIControlStateSelected?

+1
Apr 05 '14 at 18:35
source share

For Swift 2.0:

 let btnObject : UIButton = UIButton() btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22) btnObject.setTitle("Button Title", forState: UIControlState.Normal) btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13) btnObject.titleLabel?.textColor = UIColor.whiteColor() btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1) btnObject.titleLabel?.textAlignment = NSTextAlignment.Center btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside) subView.addSubview(btnObject) 
+1
Dec 02 '15 at 12:42
source share

For Swift 3.0

 let button = UIButton(type: .system) button.frame = CGRect(x: 100, y: 100, width: 100, height: 100) button.setTitle("set here", for: .normal) button.addTarget(self, action: #selector(TableViewController.actionButtonTocuh), for: .touchUpInside) button.titleLabel?.textColor = #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1) view.addSubview(button) 
0
Oct 21 '16 at 8:46
source share



All Articles