108

How do you change the text of the button and disable a button in iOS?

Whitewall
  • 597
  • 1
  • 7
  • 18
Namratha
  • 16,630
  • 27
  • 90
  • 125

8 Answers8

208

Hey Namratha, If you're asking about changing the text and enabled/disabled state of a UIButton, it can be done pretty easily as follows;

[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title
[myButton setEnabled:NO]; // To toggle enabled / disabled

If you have created the buttons in the Interface Builder and want to access them in code, you can take advantage of the fact that they are passed in as an argument to the IBAction calls:

- (IBAction) triggerActionWithSender: (id) sender;

This can be bound to the button and you’ll get the button in the sender argument when the action is triggered. If that’s not enough (because you need to access the buttons somewhere else than in the actions), declare an outlet for the button:

@property(retain) IBOutlet UIButton *someButton;

Then it’s possible to bind the button in IB to the controller, the NIB loading code will set the property value when loading the interface.

zoul
  • 102,279
  • 44
  • 260
  • 354
MGunetileke
  • 2,106
  • 1
  • 13
  • 3
  • Thank you! I have UIButtons in my app but I haven't mentioned them in the code anywhere. I have used interface builder to register the target-action mechanism(to that end, i have a few IBAction methods to handle button clicks) so how do i access the buttons?? – Namratha Feb 11 '11 at 04:18
  • 1
    Just also want to add that if you want to have the @ property declared for you. In XCode 4, hold down the CTRL key, click on the button and then drag your mouse into the view's corresponding .h file. A dialogue popups up to prompt you for the property name. Then voila you'll have the property for you to use in your code! – milesmeow Dec 24 '11 at 01:01
22
[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

Use UIControlStateNormal to set your title.

There are couple of states that UIbuttons provide, you can have a look:

[myButton setTitle: @"myTitle" forState: UIControlStateApplication];
[myButton setTitle: @"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: @"myTitle" forState: UIControlStateReserved];
[myButton setTitle: @"myTitle" forState: UIControlStateSelected];
[myButton setTitle: @"myTitle" forState: UIControlStateDisabled];
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
  • I was going to +1 to this post but decided to not to. [self.eraseButton setTitle: @"Erase" forState: UIControlStateDisabled]; This doesn't dim the button. It does nothing, if you already have setEnable: as no. – coolcool1994 Jul 15 '13 at 21:57
21

If somebody, who is looking for a solution in Swift, landed here, it would be:

myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text

Documentation: isEnabled, setTitle.

Older code:

myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
mikiqex
  • 5,023
  • 2
  • 24
  • 22
10

Assuming that the button is a UIButton:

UIButton *button = …;
[button setEnabled:NO]; // disables
[button setTitle:@"Foo" forState:UIControlStateNormal]; // sets text

See the documentation for UIButton.

zoul
  • 102,279
  • 44
  • 260
  • 354
3

To Change Button title:

[mybtn setTitle:@"My Button" forState:UIControlStateNormal];
[mybtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

For Disable:

[mybtn setEnabled:NO];
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
3

In Swift 3, you can simply change the title of a button by:

button.setTitle("Title", for: .normal)

and you disable the button by:

button.isEnabled = false

.normal is the same as UIControlState.normal because the type is inferred.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
1

SWIFT 4 with extension

set:

// set button label for all states
extension UIButton {
    public func setAllStatesTitle(_ newTitle: String){
        self.setTitle(newTitle, for: .normal)
        self.setTitle(newTitle, for: .selected)
        self.setTitle(newTitle, for: .disabled)
    }
}

and use:

yourBtn.setAllStatesTitle("btn title")
Oz Shabat
  • 1,434
  • 17
  • 16
0

If you want to change the title as a response to being tapped you can try this inside the IBAction method of the button in your view controller delegate. This toggles a voice chat on and off. Setting up the voice chat is not covered here!

- (IBAction)startChat:(id)sender {
UIButton *chatButton = (UIButton*)sender;
if (!voiceChat.active) {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
                                                                   message:@"Voice Chat will become live. Please be careful with feedback if your friend is nearby."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    [voiceChat start];
    voiceChat.active = YES;
    [chatButton setTitle:@"Stop Chat" forState:UIControlStateNormal];
}
else {
    [voiceChat stop];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
                                                                   message:@"Voice Chat is closed"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    voiceChat.active = NO;
    [chatButton setTitle:@"Chat" forState:UIControlStateNormal];
}

}

voiceChat is specific to voice chat of course, but you can use your ow local boolean property to control the switch.

Tim
  • 1,108
  • 13
  • 25