1

我试图通过按一个按钮来改变不同数量的数字。我是 xcode 的新手,不知道该怎么做,任何帮助都会很好。

我希望数字更改为 15,但仅当我第二次按下按钮时。然后,我想在第三次按下时,将数字更改为 30。按 1:从“0”到“5”,按 2:从“5”到“15”,按 3:从“15”到30",我想学习如何添加不同的数量

-(IBAction)changep1:(id) sender {
p1score.text = @"5";
if (p1score.text = @"5"){

p1score.text = @"15";

//即使上述方法有效,我也不知道如何编写代码将其更改为 30。}

4

1 回答 1

0

听起来您可能想在视图控制器中添加一个属性来存储玩家 1 的分数,如下所示:

@property (nonatomic, assign) NSInteger p1Score;

然后在你的init方法中,你可以给这个属性一个初始值:

self.p1Score = 0; // you can set this to any integral value you want

然后,在您的按钮点击方法 ( changep1) 中,您可以执行以下操作:

- (IBAction)changep1:(id)sender
{
    // add 5 (or any value you want) to p1Score
    self.p1Score = self.p1Score + 5;

    // update the display text. in code below %d is replaced with the value of self.p1Score
    p1score.text = [NSString stringWithFormat:@"%d", self.p1Score];
}
于 2012-10-18T22:34:40.143 回答