views:

44

answers:

2

Hi all,

seems like a quite easy problem but I don't get it. It have two UIButtons, one is titled 'next' the other is titled 'previous'. Both are linked to the same method. All I wanna do is change the variable 'helpStatus'depending on which button is pressed:

    if([sender currentTitle] == @"next"){
        helpStatus++;
    } 
    if ([sender currentTitle] == @"previous"){
        helpStatus--;
    }

    NSLog(@"%@", [sender currentTitle]);

the logged titels are 'next' and 'previous' just like it should be but it doesn't work and I don't know why.

+3  A: 

You need to use isEqualToString otherwise you are just comparing if they are the same object, not if they are equal :)

if([[sender currentTitle] isEqualToString:@"next"]){
    helpStatus++;
} 
if ([[sender currentTitle] isEqualToString:@"previous"]){
    helpStatus--;
}
willcodejavaforfood
Just to clarify, that means they are the same object in memory, not the same string. :)
Geoff Baum
@Geoff Baum - Thank you :)
willcodejavaforfood
+2  A: 

I would not use the string. What happens if you decide to change the label? Consider using a tag instead.

e.g. b

 button.tag = 100;

    ...

- (void)buttonPressed:(id)sender {
    UIButton *button = (UIButton*)sender;

    if(button.tag == 100) {

    }
}

or in your case (I am kidding, sort of), even:

button1.tag = 1;
button2.tag = -1;

        ...

- (void)buttonPressed:(id)sender {
   UIButton *button = (UIButton*)sender;
   helpStatus+= button.tag;
Joseph Tura