Manually setting a UIButton state
-
03-07-2019 - |
Question
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.
Solution
Did you try button.enabled = NO;
?
OTHER TIPS
there are also the states:
button.highlighted = NO;
button.selected = NO;
Isn't it just the state
property? It's in the UIControl
class which is the superclass of UIButton
.
Edit: oops, no it isn't. The docs say "This attribute is read only—there is no corresponding setter method."
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....
for Swift 3 you can use
button.isSelected = true
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)
button.state == .selected
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