It doesn't work because you will be notified just by one button press. If you for example press a button with tag 0, then the buttonPressed:
method will be called with button 0 as an argument. You are testing if button is selected twice in the if expression: button.selected
. But that will both times check only the recently pressed button.
if ((button.tag==0 && button.selected) || (button.tag==12 && button.selected))
you have to test each button state independently...
You can for example make two outlet variables that store pointer to buttons. Put that in your view controller header file:
@property (weak, nonatomic) IBOutlet UIButton *button0;
@property (weak, nonatomic) IBOutlet UIButton *button12;
Then use the layout designer to connect your outlets to those buttons. And after that you can get to .selected attribute of any of them.
- (IBAction)buttonPressed:(UIButton *)button {
button.selected = !button.selected; // i'm not sure of this line
if ((button.tag==0 || button.tag==12)
{
if( button0.selected || button12.selected )
{ // one of them is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"1"];
} else { // none is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"0"];
}
}
}
Or if you want to avoid those outlets, then check out this question. You could get a view, for example some UIButton by its tag. If you have a view attribute in your view controller you could write something like this:
- (IBAction)buttonPressed:(UIButton *)button {
button.selected = !button.selected; // i'm not sure of this line
if ((button.tag==0 || button.tag==12)
{
UIButton *button0 = (UIButton *)[self.view viewWithTag:0];
UIButton *button12 = (UIButton *)[self.view viewWithTag:12];
if( button0.selected || button12.selected )
{ // one of them is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"1"];
} else { // none is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"0"];
}
}
}