开发者

UIButton sender title problem

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.


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--;
}


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜