20

I UIButton using + buttonWithType:

What I need to figure out is how to manually change the button state. There are times when I need it to be set to "disabled."

I read through the UIButton documentation but I cannot seem to find anything about manually setting a button state.

Any thoughts would be greatly appreciated.

iDeveloper
  • 940
  • 1
  • 10
  • 45
acreek
  • 325
  • 1
  • 3
  • 8
  • To supplement what Ben said, check out the enabled property in UIControl: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/occ/instp/UIControl/enabled – Athena Jan 01 '09 at 19:36

7 Answers7

49

Did you try button.enabled = NO;?

Swift 5.0

button.isEnabled = false
Sergio
  • 1,610
  • 14
  • 28
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
28

there are also the states:

   button.highlighted = NO;
   button.selected = NO;
roberthuttinger
  • 1,172
  • 1
  • 17
  • 31
4

Objective C:

button.selected = Yes;
button.highlighted = NO;
button.enabled = Yes;

Swift 4:

button.isSelected = true 
button.isEnabled = true

Also you can use:(swift 4)

 if (button.state == .selected) {
   //do something
 }
Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
3

You can manually set state of UIButton.

UIButton *btnCheck=[UIButton buttonWithType:UIButtonTypeCustom];

if(btncheck isselected])
{
    btncheck.selected=FALSE;
}
else
{
    btncheck.selected=TRUE;
}

You can do operation on UIButton as per your requirement like perform some action when UIButton is selected and while not selected.

Hope this will help you....

Birju
  • 1,132
  • 11
  • 32
2

for Swift 3 you can use

button.isSelected = true

Firas Shrourou
  • 625
  • 8
  • 19
0

For anyone arriving here looking to change the 'state' of the button (as opposed to 'enabled'). Stephen is correct "This attribute is read only—there is no corresponding setter method."

What you really want to set is the state of the buttons cell.

[[myNSButtonOutlet cell] setState: NSOnState];  //Options NSOnState, NSOffState, NSMixedState
Lorne K
  • 109
  • 7
0

To accommodate the needs of setting a different button look when it's selected, set isSelected = true.

Here is an example of setting the state, and changing the UIButton appearance based on that state, using a quiz game environment:

if sender.titleLabel?.text == correctAnswer {
    sender.isSelected = true
    // Set title color and image based on 'selected' state.
    sender.setTitleColor(.systemBlue, for: .selected)
    sender.setImage(Utilities.sharedInstance.returnAnswerButtonImage(for: .correctlyAnswered).withRenderingMode(.alwaysOriginal), for: .selected)
    sender.backgroundColor = .white
    sender.tintColor = .systemBlue
    // moveToNextQuestion()
} else {
    sender.isSelected = true
    sender.setTitleColor(.systemBlue, for: .selected)
    sender.setImage(Utilities.sharedInstance.returnAnswerButtonImage(for: .incorrectlyAnswered).withRenderingMode(.alwaysOriginal), for: .selected)
    sender.backgroundColor = .red
    sender.tintColor = .systemBlue
}
andrewlundy
  • 1,073
  • 15
  • 22