如何将标签文本设置为 NSMutableArray 中的内容?
我试图了解当您按下按钮时如何将标签设置为数组中的文本。当我按下按钮时,标签消失,然后什么也没有出现。代码中没有崩溃。
相关代码:
-(void)setupArray {
wordArray = [[NSMutableArray alloc] init];
[wordArray addObject:@"test1"];
[wordArray addObject:@"test2"];
[wordArray addObject:@"test3"];
}
- (IBAction)start:(id)sender {
int value = (arc4random() % 3) + 1;
self.typeThis.text = [self.wordArray objectAtIndex:value];
}
typeThis 是标签名称,我想我已经连接了所有内容,即设置了按钮/委托/等...我不明白为什么它不起作用。有人可以帮忙吗?
I am trying to understand how to set a label to be the text from an array when you press the button. When I press the button, the label disappears, and then nothing comes up. No crashes in code.
Relevant code:
-(void)setupArray {
wordArray = [[NSMutableArray alloc] init];
[wordArray addObject:@"test1"];
[wordArray addObject:@"test2"];
[wordArray addObject:@"test3"];
}
- (IBAction)start:(id)sender {
int value = (arc4random() % 3) + 1;
self.typeThis.text = [self.wordArray objectAtIndex:value];
}
typeThis is the label name, and I think I have hooked up everything already, i.e. set up the buttons/delegates/etc...I don't understand why it isn't working. Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到您已正确绑定所有内容并且您不在 ARC 之下。这是可能导致您出现问题的一件事。
当您
alloc
atingwordArray
时,您可以尝试使用以下代码片段。如果您在 ARC 下,您可以尝试
self.wordArray = [NSMutableArray array];
然后将对象添加到
self.wordArray
即[self.wordArray addObject:@"测试1"];
。这是一些关于arc4random的解释
()
。编辑:
这是自动引用计数和引用自公共 iOS 5 页面:
可以检测 ARC 是否启用。只需将以下代码片段添加到任何需要 ARC 的文件中即可。
更多信息:
http://clang.llvm.org/docs/LanguageExtensions.html#__has_feature_extension
HTH。
considering you have bound everything properly and you are not under ARC. Here is a thing that might cause you the issue.
when you are
alloc
atingwordArray
you can try using following code snippet.if you are under ARC you can try
self.wordArray = [NSMutableArray array];
then add objects to
self.wordArray
i.e.[self.wordArray addObject:@"test1"];
. Here is some explanation aboutarc4random()
.EDIT :
Here's a public spec for Automatic Reference Counting and a quote from the public iOS 5 page:
It is possible to detect if ARC is enabled. Simply add the following snippet to any file that requires ARC.
More info :
http://clang.llvm.org/docs/LanguageExtensions.html#__has_feature_extension
HTH.
你的“+1”给出的结果在 1 到 3 之间,而你的索引是从 0 到 2,所以我预计它每 3 次就会出错一次。
Your '+1' is giving you a result between 1 and 3, and your indexes are from 0 to 2, so I'd expect it to go wrong one time in 3.
这是在 ARC 下吗?如果是这样,wordArray 是否声明为强?
Is this under ARC? If so, is wordArray declare as strong?