在变量中添加值(If语句)

时间:2015-06-05 07:22:48

标签: ios objective-c iphone

我试图解决这个问题,我有一个计数应用程序,并希望它增加45。假设用户点击+ 5次,他们只能减去-5,因为它等于0。

这是我的if语句,但它不起作用,它进入底片。 有人可以帮忙吗?它没有来找我。这是(if count >= 0)

-(IBAction)upButton45:(id)sender {
    xCount1 +=1;
    countNumber45 +=45;
    x45Label.text = [NSString stringWithFormat:@"45x%i", xCount1];
    totalWeight += 45;
    TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", totalWeight];
}


-(IBAction)downButton45:(id)sender {
    xCount1 -= 1;
    countNumber45 -= 45;
    x45Label.text = [NSString stringWithFormat:@"45x%i", xCount1];
    if (countNumber45 <= 0) {
        countNumber45 = 0;
        xCount1 = 0;

        x45Label.text = @"";
    }
    if (xCount1 >= 0) {
        totalWeight -= 45;
        TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", totalWeight];
    }
}

2 个答案:

答案 0 :(得分:0)

-(IBAction)upButton45:(id)sender 
{
    if(xCount + 1 <= 45) //Your max allowed tap
    {   
        xCount1 +=1;
        countNumber45 +=45;
        x45Label.text = [NSString stringWithFormat:@"45x%i", xCount1];
        totalWeight += 45;
        TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", totalWeight];
    }

}


-(IBAction)downButton45:(id)sender
{
    if(xCount - 1 >= 0)
    {
        xCount1 -= 1;
        countNumber45 -= 45;
        x45Label.text = [NSString stringWithFormat:@"45x%i", xCount1];
        totalWeight -= 45;
        TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", totalWeight];
    }
}

答案 1 :(得分:0)

-(IBAction)upButton45:(id)sender
{
    //Assuming that the lable always hold value and have "0" as start value, and number is at first position
    NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:@" "];
    int currentValue =  [(NSString *)stringList[0] intValue];

    currentValue = currentValue + 45;
    TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", currentValue];
}


-(IBAction)downButton45:(id)sender
{
    //Assuming that the lable always hold a int value and number is at first position
    NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:@" "];
    int currentValue =  [(NSString *)stringList[0] intValue];

    if (currentValue != 0) {
         currentValue = currentValue - 45;
    }
    TotalWeightLabel.text = [NSString stringWithFormat:@"%d LBS", currentValue];
}