2

I have two buttons, one with tag 0 and the other with tag 12, and an NSMutableString buttonPressings. I want the next: if either one of the buttons is selected, or both of them are selected, I set the first digit in my string to 1. If neither one is selected, I set it to 0.

My code is the next:

- (IBAction)buttonPressed:(UIButton *)button {
button.selected = !button.selected;

if ((button.tag==0 && button.selected) || (button.tag==12 && button.selected))
{
    NSRange range = {0,1};
    [buttonPressings replaceCharactersInRange:range withString:@"1"];
}

else if ((button.tag==0 && !button.selected) && (button.tag==12 && !button.selected))
{
     NSRange range = {0,1};
    [buttonPressings replaceCharactersInRange:range withString:@"0"];
}

}

It's not working. What is wrong here? Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Automatika
  • 43
  • 5

2 Answers2

1

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"];
        }      
    }
}
Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
  • Thanks. Can you explain a bit more clearly why my way is not working? "will be notified just by one button press, you have to test each button state independently... " - I didn't completely understand it. – Automatika Aug 01 '13 at 12:52
  • I thought of that way right away, but is there any possibility to implement it my way? Having only one outlet to all the buttons – Automatika Aug 01 '13 at 12:53
  • i've edited my question, i would then like to know if it helped – nio Aug 01 '13 at 13:31
  • You're right, the outlets way seems to be easier. However, I still have two questions: 1) why do we need tags then? Why not just `if (button0.selected) {...}`? 2) This piece of code doesn't seem to work here, either with tags or without them. XCode gives me: "Use of undeclared identifier 'button0'; did you mean '_button0'?", while I have created the outlets and connected them to the buttons. Sorry if my questions are too noobish and thanks a lot for your help. – Automatika Aug 02 '13 at 06:46
  • Ok I tried different things and it turns out we need to add `self.` here, so it gets `if(self.button0.selected || self.button12.selected )`. At least it works this way. Can somebody explain me why we need `self.` here? – Automatika Aug 02 '13 at 16:17
  • 1
    the self.button0 is actually call to button0 property getter, maybe _button0 which is the variable behind button0 property will work as ordinary variable. – nio Aug 03 '13 at 20:16
1

Check your code once again.

else if ((button.tag==0 && !button.selected) && (button.tag==12 && !button.selected))

its checking both condion button.tag for 0 and 12 , so for this you have to make object of you both button.

or

Try this

- (IBAction)buttonPressed:(UIButton *)button
{
    button.selected = !button.selected;

    if (button.tag==0 || button.tag==12)
        {
            UIButton *firstBtn = (UIButton *)[button viewWithTag:0];
            UIButton *secondBtn = (UIButton *)[button viewWithTag:12];

            if( firstBtn.selected || secondBtn.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"];
            }      
        }
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • Are you sure? In first "if", right, I place ||, because it's "one button or another". In "else if" though, it's "neither one", so && is fine, isn't it? – Automatika Aug 01 '13 at 12:55
  • thanks for your answers. `button.selected = !button.selected;` - can you comment on what is this line for? – Automatika Aug 02 '13 at 07:25
  • 1
    this line is used if you again press the button.. then it set selected yes or no – Rajneesh071 Aug 02 '13 at 07:46
  • Thank you. Can you take a look at the other answer as well? Maybe you can give me some heads up about the outlet way. – Automatika Aug 02 '13 at 10:25