按钮按1按钮按下所有if语句 - 目标C

时间:2014-07-25 12:09:32

标签: objective-c button if-statement uibutton ibaction

如果这是一件简单的事情我很抱歉,我仍然是客观C的新手。 我有一个按钮,我试图改变每次按下它的图像和文本,但我的问题是,它一直在经历每一次,直到IBAction结束。 我需要做什么,每次点击按钮只做1“if”? 谢谢

-(IBAction)Button1:(UIButton *)sender {
if (button1TapCount == 0) {
    button1TapCount = 1;
    textString = (@"Button 1 Pressed");
    button1ImageName  = (@"test.png");
    [self buttonschange];
}
if (button1TapCount == 1) {
    button1TapCount = 2;
    textString = (@"Button 1 Pressed twice");
    button1ImageName  = (@"test1.png");
    [self buttonschange];
}
if (button1TapCount == 2) {
    button1TapCount = 3;
    textString = (@"Button 1 Pressed third time");
    button1ImageName  = (@"test2.png");
    [self buttonschange];
}
}

3 个答案:

答案 0 :(得分:1)

这是因为您没有使用else并且您正在更改每个button1TapCount语句中的if,以便 next { {1}}将其解析为真。

使用:

if

答案 1 :(得分:0)

在第一个如果您将button1TapCount设置为1,那么第二个if也是如此。我认为你需要的是其他如果:

-(IBAction)Button1:(UIButton *)sender {
    if (button1TapCount == 0) {
        button1TapCount = 1;
        textString = (@"Button 1 Pressed");
        button1ImageName  = (@"test.png");
        [self buttonschange];
    }
    else if (button1TapCount == 1) {
        button1TapCount = 2;
        textString = (@"Button 1 Pressed twice");
        button1ImageName  = (@"test1.png");
        [self buttonschange];
    }
    else if (button1TapCount == 2) {
        button1TapCount = 3;
        textString = (@"Button 1 Pressed third time");
        button1ImageName  = (@"test2.png");
        [self buttonschange];
    }
}

答案 2 :(得分:0)

使用 if-else 返回切换

-(IBAction)Button1:(UIButton *)sender {
if (button1TapCount == 0) {
    button1TapCount = 1;
    textString = (@"Button 1 Pressed");
    button1ImageName  = (@"test.png");
    [self buttonschange];
return;
}
if (button1TapCount == 1) {
    button1TapCount = 2;
    textString = (@"Button 1 Pressed twice");
    button1ImageName  = (@"test1.png");
    [self buttonschange];
return;
}
if (button1TapCount == 2) {
    button1TapCount = 3;
    textString = (@"Button 1 Pressed third time");
    button1ImageName  = (@"test2.png");
    [self buttonschange];
return;

}
}