Xcode - 如何获取多次更改标签文本的按钮 [英] Xcode - How to get button to change label text multiple times
问题描述
我试图通过按一个按钮让数字按不同的数量变化。我是xcode的新手,不知道怎么做,任何帮助都会很好。
I am trying to have numbers change by different amounts, by the press of one button. I am new to xcode and do not know how to do this, any help would be nice.
我希望这个数字改为15,但只有当我按下按钮第二次。然后,在第三次按下时,我想要更改数字30.
按1:从0到5,
按2:从5到15,
按3:从15到30,我想学习如何添加不同金额
I want the number to change to 15, but only when I press the button for a second time. Then, I would like, upon a third press, for the number to change 30. press 1: from "0" to "5", press 2: from "5" to "15", press 3: from "15" to 30", I want to learn how to add different amounts
-(IBAction)changep1:(id) sender {
p1score.text = @"5";
if (p1score.text = @"5"){
p1score.text = @"15";
//即使以上工作,我也不知道怎么写将其更改为30的代码。}
//Even if the above worked, I do not know how I would write the code to change it to 30. }
推荐答案
听起来您可能想要在视图控制器中添加一个属性来存储玩家1的得分,如下所示:
It sounds like you probably want to add a property to your view controller to store Player 1's score, something like this:
@property (nonatomic, assign) NSInteger p1Score;
然后在 init
方法中,您可以为此属性赋予初始值:
Then in your init
method, you can give this property an initial value:
self.p1Score = 0; // you can set this to any integral value you want
然后,在你的按钮点击方法( changep1
)你可以这样做:
Then, in your button tap method (changep1
) you can do something like this:
- (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 - 如何获取多次更改标签文本的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!