问答游戏/单击按钮将文本添加到标签



我正在做一个测验。它的工作原理是这样的,当你回答一个问题时,你必须点击一些带有文字的按钮才能穿上衣服。

示例 () = 按钮

(G) (L) (O) (R) (I) (O) (

U) (S)

他们必须在那个标签中正确拼写光荣。当他们点击 answear 按钮时,如果标签上写着 Glorious,他们就会进入下一个级别。如果它说了其他类似 Gloirous 的东西,就会出现一条说错误 answear 的消息。

问题 :

问题是当我单击按钮时,它会向标签添加文本。

但是当我单击另一个按钮时,以前的文本消失了,新的文本进来了。

我希望你们明白我在这里想要什么!我的英语不是最好的,但如果你想让我发布一些代码,我可以这样做!;)

您需要保存以前的文本,并将新文本附加到上一个文本。

label.setText(label.text() + newText)

试试这个:

[yourLabel setText:[yourLabel.text stringByAppendingString:@"%@", yourButton.text]];

不要将文本保存到标签。在标签中显示文本,但将其存储在其他位置。

创建一个实例变量"answerString"。将每个字母附加到答案字符串,然后将答案字符串显示到标签:

NSString *answerString; 

此外,不要为每个按钮编写不同的 IBAction 方法。在每个按钮上放置数字标签,将它们附加到相同的方法,并使用标签号来确定按下了哪个按钮。像这样:

typedef NS_ENUM(NSInteger, buttonTags)
{
 aButton = 100,
 bButton = 101,
 cButton = 102
//and so on for the entire alphabet, or for the letters you need
}
- (IBAction) buttonAction: (UIButton *) sender;
{
  int button_id = sender.tag;
  switch (button_id)
  {
    case aButton:
      answerString = [answerString stringByAppendingString: @"a"];
      break;
    case bButton:
      answerString = [answerString stringByAppendingString: @"b"];
      break;
  }
  answerLabel.text = answerString
}

如果你有所有 26 个字母的按钮,那么对标签号做一些数学运算以获得每个字符的 unicode 值可能更有意义,而不是有一个包含 26 个案例的 switch 语句,但你明白了。

最新更新