Simple, but not working! Can someone please help? All I want is to just toggle the selection of a UIButton, with the code below:
-(IBAction) buttonPressed:(id)sender {
if ([self.mybutton isSelected]) {
NSLog(@"selected");
[self.mybutton setSelected:NO];
} else {
NSLog(@"not selected");
[self.mybutton setSelected:YES];
}
NSLog([self.mybutton isSelected] ? @"Yes" : @"No");
}
Though I press the button any number of times, every time, what gets printed is ‘not selected’ and ‘No’.
Why isn’t the UIButton getting set to ‘selected’ state?
As your code seems to be correct, you can make it simpler this way:
Why your code is not working properly? Probably
self.mybuttonisnil(you can check that out). Check your outlet connection and the property definition…Ref.: Sending a message not a
nilobject, returns eithernilor the default value if it’s a simple type (0 forint,NOforBOOL, etc..)