设置按钮标题和来自可变数组或字典的操作?

发布于 2024-12-20 06:24:47 字数 1446 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-12-27 06:24:47

正确的输入是指针,不用担心。我能够获得一个按钮来显示以下代码,该代码改编自您的示例。

NSMutableArray* choiceValue = [NSMutableArray new];
[choiceValue addObject:[NSString stringWithFormat:@"%d", 42]];
[button1 setTitle:[choiceValue objectAtIndex:0] forState:UIControlStateNormal]; // Assumes button1 is already instantiated and visible

您确定数组中的字符串已正确创建吗?您使用 %d 来指定它们,这使我相信它们是整数。您可以验证这一点并使用类似的字符串调用将它们注销吗?您可以使用硬编码字符串作为按钮标题吗?

NSLog([NSString stringWithFormat:@"%d", incorrectOne]); // Test that your string is created correctly
[button1 setTitle:@"42" forState:UIControlStateNormal]; // Test that your button is capable of displaying a title

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.

NSMutableArray* choiceValue = [NSMutableArray new];
[choiceValue addObject:[NSString stringWithFormat:@"%d", 42]];
[button1 setTitle:[choiceValue objectAtIndex:0] forState:UIControlStateNormal]; // Assumes button1 is already instantiated and visible

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?

NSLog([NSString stringWithFormat:@"%d", incorrectOne]); // Test that your string is created correctly
[button1 setTitle:@"42" forState:UIControlStateNormal]; // Test that your button is capable of displaying a title
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文