如何在每次点击UIButton时增加UILabel值?

时间:2016-02-05 12:01:55

标签: ios objective-c

我正在使用UILabel并为其分配一些字符串值,现在我想在每次单击UIButton时更新其值。见我IBAction代码。

6

当我第一次点击按钮时,我的逻辑执行正常。但是,当我再次点击时,代码不会被执行,但每次点击都会打印出来。

2 个答案:

答案 0 :(得分:0)

试试这个:

<。>文件中的

@property (strong, nonatomic) int labelValue;
<。>文件中的

- (void)viewWillAppear{
  self.labelValue = 0;
}
- (IBAction)buttonClicked:(UIButton *)sender{
    self.labelValue++;
    [self.label setText:[NSString stringWithFormat:@"%d", self.labelValue]];
}

答案 1 :(得分:0)

您的代码没有意义。您的IBAction会从newprice计算increseprice+updateprice。如果incresepriceupdateprice未更改,则这些值的总和也不会更改。

您需要一个保存当前值的实例变量,当用户点击您的按钮时,您需要将增加值添加到之前的值并更新总数

相反,你总是说

newprice = increseprice + updateprice

你没有保存新的总数。再次单击该按钮,增加的价格更新价格都不会改变,因此newprice仍然是相同的值。