使用动态整数更改按钮按下时标签的文本

Changing the text of a label on button press with dynamic integer

我正在按照教程创建一个简单的游戏,您可以在其中点击一个按钮,然后游戏会计算您在进行过程中按下按钮的次数。分数以标签的形式显示在屏幕上。

我可以做到,当您按下按钮时,标签文本会变为 'Pressed!'。但是当我尝试使用格式说明符添加不断变化的分数时,我无法让标签发生变化。

-(IBAction)buttonPressed{
  count ++;
  //scoreLabel.text =@"pressed";
  scoreLable.text = [NSString stringWithFormat:@"Score\n%i", count];
  }

如有任何帮助,我们将不胜感激。谢谢!

-(IBAction)buttonPressed : (id) sender{

    UIButton * btnPressed = (UIButton *)sender;

    int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];

    NSLog(@"Button value = %d", buttonValue);

    //scoreLabel.text =@"pressed";
    scoreLable.text = [NSString stringWithFormat:@"Score\n%i", count];
}

首先你可以设置static int count = 0;

您可能没有为多行文本设置标签。尝试去掉格式字符串中的“\n”。除此之外,您需要告诉我们发生了什么。您是否设置了断点以确保您的按钮 IBAction 被调用?您是否检查过以确保 "scoreLable" (原文如此)不为零?细节决定成败。