3

I have a UILabel called label, and you can add or subtract 1 from it using two buttons. When you subtract all the way down to 0, I want the minus button to stop working. And if the value is added, I want the minus button to work again. Here is the method/code I'm using for the add/subtract button:

- (IBAction)addButton1:(id)sender {
    [label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];    
}

the code is the same for both the add/subtract methods. Except the +1 at the end is a -1.

I tried:

- (IBAction)addButton1:(id)sender {
    int val = [label.text intValue];

    [label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];    

    if(val - 1 <= 0) { 
        UIButton *button = (UIButton *)sender;
        [button setEnabled:NO]; 
    } 
}
jscs
  • 63,694
  • 13
  • 151
  • 195
  • int val = [label.text intValue]; if(val - 1 <= 0) { UIButton *button = (UIButton *)sender; [button setEnabled:NO]; } // set label text = 0; – Kenny Lim Sep 11 '11 at 22:43
  • @Kenny Lim ok i tried this. But it makes the addButton: method to stop working when the value is = to -2. Here ill post my code in the question could you please check it over to see if i did it correctly? Thanks for the reply!! ;D – jessica simpson Sep 11 '11 at 22:53
  • it looks like you are disabling the `+` button while it seems to me you want to disable the `-` button – Jean-Denis Muys Sep 11 '11 at 22:56
  • @jean-Denis Muys ha i know i totally just caught that! Thanks for the reply! But i added the code to the `-` method and it works and it stops if the label is = to 0, but when i add to the value lets say we press the button 3 times so now the value is = to 3 and i hit the `-` button again and it does nothing so how can i get the method to start up, or start working again if the value isnt 0? Thanks a lot! – jessica simpson Sep 11 '11 at 22:58

1 Answers1

2

Try

- (IBAction)addButton:(id)sender {

    if ( [[label text] intValue] == 0) 
        [minusButton setEnabled:YES];

    [label setText:[NSString stringWithFormat:@"%d",[label.text intValue] +1]];    
}


- (IBAction)subButton:(id)sender {

    [label setText:[NSString stringWithFormat:@"%d",[label.text intValue] -1]];

    if ( [[label text] intValue] == 0) 
        [minusButton setEnabled:NO];

}

You simply need to keep the pointer to the minus button (simply create an IBOutlet and then link it to the button using IB)

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • hmm... i tried this and still no luck:( and i cant connect an outlet in IB because i created the buttons and the label programatically. Could i do this another way?? Thanks for the reply! – jessica simpson Sep 11 '11 at 23:26
  • 1
    If you created the buttons programmatically it's even simpler! :) You just need to keep the pointer of the button. You probably did something like `UIButton *minusButton = [[UIButton alloc] initWithFrame:...];` isn't it? Well, just create a `UIButton *minusButton` field in your class, then, when you create the button, use `minusButton = ...`. This way you'll keep the pointer to the button for a later use (such enabling/disabling) – Manlio Sep 11 '11 at 23:36