Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode-如何使用按钮多次更改标签文本_Xcode_Button_If Statement - Fatal编程技术网

Xcode-如何使用按钮多次更改标签文本

Xcode-如何使用按钮多次更改标签文本,xcode,button,if-statement,Xcode,Button,If Statement,我试着通过按下一个按钮,让数字以不同的数量变化。我是新来的xcode,不知道如何做到这一点,任何帮助都会很好 我想把号码改为15,但只有当我第二次按按钮时才可以。然后,我想在第三次按下时,把号码改为30。 按1:从“0”到“5”, 按2:从“5”到“15”, 按3:从“15”到“30”,我想学习如何添加不同的量 -(IBAction)changep1:(id) sender { p1score.text = @"5"; if (p1score.text = @"5"){ p1score.tex

我试着通过按下一个按钮,让数字以不同的数量变化。我是新来的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。}

听起来您可能想向视图控制器添加一个属性来存储玩家1的分数,类似这样:

@property (nonatomic, assign) NSInteger p1Score;
- (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];
}
然后在
init
方法中,可以为该属性指定初始值:

self.p1Score = 0; // you can set this to any integral value you want
然后,在按钮点击方法(
changep1
)中,可以执行以下操作:

@property (nonatomic, assign) NSInteger p1Score;
- (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];
}
可能重复的