设置按钮标题和来自可变数组或字典的操作?
我有 4 个可能的答案:3 个错误,1 个正确。
我将 4 个答案存储在“choiceValue”可变数组中作为 NSStrings。
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectOne]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectTwo]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectThree]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", correctAnswer]];
然后,我将打乱数组顺序(不是本问题主题的一部分),
我想根据(打乱的)数组索引分配 4 个按钮的标题 (setTitle)。
我尝试了以下代码,但我相信我错过了一些东西:
[button1 setTitle:[choiceValue objectAtIndex:0] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:[choiceValue objectAtIndex:3] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(handleCorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button3 setTitle:[choiceValue objectAtIndex:1] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button4 setTitle:[choiceValue objectAtIndex:2] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
理想情况下,因为我根据答案是否正确或不正确而对“action”调用不同的方法,所以我相信使用字典(一对“title”)可以更好地完成此操作/“方法名称”)。 但现在如果我能让它与可变数组一起工作——那就太好了。
非常感谢帮助!
I have 4 possible answers: 3 incorrect and 1 correct.
I store the 4 answer in the "choiceValue" mutable array as NSStrings.
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectOne]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectTwo]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", incorrectThree]];
[choiceValue addObject:[NSString stringWithFormat:@"%d", correctAnswer]];
I will then shuffle the array order (not part of this question topic)
I want to assign the titles (setTitle) of the 4 buttons based on the (shuffled) array index.
I tried the following code but I believe I'm missing something:
[button1 setTitle:[choiceValue objectAtIndex:0] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:[choiceValue objectAtIndex:3] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(handleCorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button3 setTitle:[choiceValue objectAtIndex:1] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
[button4 setTitle:[choiceValue objectAtIndex:2] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(handleIncorrectAnswer:) forControlEvents:UIControlEventTouchUpInside];
Ideally, since I call different methods on "action" depending if the answer is correct or incorrect, I believe this would be better done using a dictionary (a pair "title" / "method name").
But for now if I can get it working with the mutable array - it would be great.
Help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的输入是指针,不用担心。我能够获得一个按钮来显示以下代码,该代码改编自您的示例。
您确定数组中的字符串已正确创建吗?您使用 %d 来指定它们,这使我相信它们是整数。您可以验证这一点并使用类似的字符串调用将它们注销吗?您可以使用硬编码字符串作为按钮标题吗?
The correct input is a pointer, no worries there. I was able to get a button to show up with the following code, adapted from your example.
Are you sure that the strings in your array are being created correctly? You are using %d to specify them, which leads me to believe that they are ints. Can you verify this and log them out with a similar string call? Can you use a hardcoded string as the button title?