如何将标签文本设置为 NSMutableArray 中的内容?

发布于 2024-12-18 08:24:15 字数 496 浏览 4 评论 0原文

我试图了解当您按下按钮时如何将标签设置为数组中的文本。当我按下按钮时,标签消失,然后什么也没有出现。代码中没有崩溃。

相关代码:

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

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

发布评论

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

评论(3

别想她 2024-12-25 08:24:15

考虑到您已正确绑定所有内容并且您不在 ARC 之下。这是可能导致您出现问题的一件事。
当您allocating wordArray时,您可以尝试使用以下代码片段。

NSMutableArray tempArray = [[NSMutableArray alloc] init];
self.wordArray = tempArray;
[tempArray release];

如果您在 ARC 下,您可以尝试 self.wordArray = [NSMutableArray array];

然后将对象添加到 self.wordArray[self.wordArray addObject:@"测试1"];。这是一些关于arc4random的解释()

编辑:
这是自动引用计数引用自公共 iOS 5 页面:

Objective-C 的自动引用计数 (ARC) 可以节省内存
管理编译器的工作。通过使用新的 Apple 启用 ARC
LLVM编译器,您将永远不需要再次键入retain或release,
极大地简化了开发过程,同时减少了
崩溃和内存泄漏。编译器完全理解
你的对象,并在每个对象不再存在时立即释放它
使用过,因此应用程序的运行速度一如既往地快,并且可预测、流畅
性能。

可以检测 ARC 是否启用。只需将以下代码片段添加到任何需要 ARC 的文件中即可。

#ifndef __has_feature
  #define __has_feature(x) 0 /* for non-clang compilers */
#endif

#if !__has_feature(objc_arc)
  #error ARC must be enabled!
#endif

更多信息:
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 allocating wordArray you can try using following code snippet.

NSMutableArray tempArray = [[NSMutableArray alloc] init];
self.wordArray = tempArray;
[tempArray release];

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 about arc4random().

EDIT :
Here's a public spec for Automatic Reference Counting and a quote from the public iOS 5 page:

Automatic Reference Counting (ARC) for Objective-C makes memory
management the job of the compiler. By enabling ARC with the new Apple
LLVM compiler, you will never need to type retain or release again,
dramatically simplifying the development process, while reducing
crashes and memory leaks. The compiler has a complete understanding of
your objects, and releases each object the instant it is no longer
used, so apps run as fast as ever, with predictable, smooth
performance.

It is possible to detect if ARC is enabled. Simply add the following snippet to any file that requires ARC.

#ifndef __has_feature
  #define __has_feature(x) 0 /* for non-clang compilers */
#endif

#if !__has_feature(objc_arc)
  #error ARC must be enabled!
#endif

More info :
http://clang.llvm.org/docs/LanguageExtensions.html#__has_feature_extension

HTH.

夜夜流光相皎洁 2024-12-25 08:24:15

你的“+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.

英雄似剑 2024-12-25 08:24:15

这是在 ARC 下吗?如果是这样,wordArray 是否声明为强?

Is this under ARC? If so, is wordArray declare as strong?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文