Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if UIButton is selected

Is there any way to keep track the state of a UIButton whether it is selected or not? I tried accessing the selected property but it doesn't seem to work, seems to work only for UISwitch

like image 475
adit Avatar asked Dec 17 '22 01:12

adit


2 Answers

You need to set the state to selected if you want to use it like a toggle

- (void)buttonTapped:(UIButton *)button;
{
    button.selected = ![button isSelected];
}

then you can just query it like normal

[self.button isSelected];
like image 77
Paul.s Avatar answered Dec 27 '22 06:12

Paul.s


Yes some UIButtons can have a selected state, which may only be momentary. However UIButtons inherit from UIControls which have a selected property. You can query to see if you button is selected using:

if([myButton isSelected])
    NSLog(@"Selected!")
like image 23
Kyle Richter Avatar answered Dec 27 '22 07:12

Kyle Richter