创建一个按钮,为标签添加/减去一个值,该标签将在标签上显示相应的图片

时间:2014-07-07 12:55:13

标签: ios uibutton uiimage uilabel

我是自学,不知道我的代码有什么问题,以及如何更简洁的代码?

我希望数字仅设置在1,2,3,4之间 所以我不想要负数或任何大于4的数字 数字在模拟器上显示出很棒且正确,但图片搞砸了 它回到1,2,3,4然后在回到4,3,2,1时会变得混乱。

- (IBAction)plusLevelPressed:(id)sender {
    int intValueFromFirstLabel = [_lvlLabel.text intValue];

    if(intValueFromFirstLabel != 4)
    {
        _lvlLabel.text = [NSString stringWithFormat:@"%d",[_lvlLabel.text intValue]+1];
    }
    //change image according to lvl selection
    if(intValueFromFirstLabel == 0)
    {
        UIImage *lvl1 = [UIImage imageNamed:@"lvl1.jpg"];
        [_lvlMap setImage:lvl1];
    }

    if(intValueFromFirstLabel == 1)
    {
        UIImage *lvl2 = [UIImage imageNamed:@"lvl2.jpg"];
        [_lvlMap setImage:lvl2];
    }

    if(intValueFromFirstLabel == 2)
    {
        UIImage *lvl3 = [UIImage imageNamed:@"lvl3.jpg"];
        [_lvlMap setImage:lvl3];
    }

    if(intValueFromFirstLabel == 3)
    {
        UIImage *lvl4 = [UIImage imageNamed:@"lvl4.jpg"];
        [_lvlMap setImage:lvl4];
    } }

- (IBAction)minusLevelPressed:(id)sender {
    int intValueFromFirstLabel = [_lvlLabel.text intValue];

    if(intValueFromFirstLabel != 1)
    {
        _lvlLabel.text = [NSString stringWithFormat:@"%d",[_lvlLabel.text intValue]-1];
    }

    //change image according to lvl selection
    if(intValueFromFirstLabel == 0)
    {
        UIImage *lvl1 = [UIImage imageNamed:@"lvl1.jpg"];
        [_lvlMap setImage:lvl1];
    }

    if(intValueFromFirstLabel == 1)
    {
        UIImage *lvl2 = [UIImage imageNamed:@"lvl2.jpg"];
        [_lvlMap setImage:lvl2];
    }

    if(intValueFromFirstLabel == 2)
    {
        UIImage *lvl3 = [UIImage imageNamed:@"lvl3.jpg"];
        [_lvlMap setImage:lvl3];
    }

    if(intValueFromFirstLabel == 3)
    {
        UIImage *lvl4 = [UIImage imageNamed:@"lvl4.jpg"];
        [_lvlMap setImage:lvl4];
    }

}

我正在学习如何为自己建立一个更好的职业,所以我非常感谢所有的帮助! 谢谢!

1 个答案:

答案 0 :(得分:0)

你是偶然的,去吧:

[self plusLevel]; intValueFromFirstLabel == 0, _lvlLabel == 1 
[self plusLevel]; intValueFromFirstLabel == 1, _lvlLabel == 2
[self plusLevel]; intValueFromFirstLabel == 2, _lvlLabel == 3
[self plusLevel]; intValueFromFirstLabel == 3, _lvlLabel == 4

然后

[self minusLevel]; intValueFromFirstLabel == 4, _lvlLabel == 3 * 
[self minusLevel];
[self minusLevel];
[self minusLevel];

当你开始通过minusLevel时,第一次运行的intValueFromFirstLabel是4,因为它取_lvlLabel HOWEVER的值,你的minusLevel没有条件

if(intValueFromFirstLabel == 4)
{

}

所以你看到我认为当你减去水平时,它似乎落后1级,是吗?

无论哪种方式,我认为您可以将代码缩短为更短:

-(IBAction)plusLevelPressed:(id)sender
{
    int intValueFromFirstLabel = [_lvlLabel.text intValue];

    // only update level image if level is within 0 to 3
    if(intValueFromFirstLabel > -1 && intValueFromFirstLabel < 4)
    {
        [self setLevelImageForLevelNumber:intValueFromFirstLabel];

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

-(IBAction)minusLevelPressed:(id)sender
{
    int intValueFromFirstLabel = [_lvlLabel.text intValue];

    // only update level image if level is within 0 to 3
    if(intValueFromFirstLabel > -1 && intValueFromFirstLabel < 4)
    {
        [self setLevelImageForLevelNumber:intValueFromFirstLabel];

        _lvlLabel.text = [NSString stringWithFormat:@"%d",[_lvlLabel.text intValue]-1];
    }
}

-(void)setLevelImageForLevelNumber:(int)levelNumber
{
    NSString *strImageName = [[NSString alloc] initWithFormat:@"lvl%d.jpg", levelNumber + 1];
    UIImage *lvlImage = [UIImage imageNamed:strImageName];
    [_lvlMap setImage:lvlImage];
}