Xcode中 - 如何获得按钮来更改标签文本多次(Xcode - How to get button

2019-10-17 08:01发布

我想有不同量的数字变化,通过一个按钮的新闻。 我是新来的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}

Answer 1:

这听起来像你可能要一个属性添加到您的视图控制器存储玩家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];
}


文章来源: Xcode - How to get button to change label text multiple times