退出方法后全局数组丢失值

发布于 2024-12-08 07:10:34 字数 588 浏览 0 评论 0原文

在 .m 文件的顶部,我将

static NSMutableArray *name;

一堆值加载到 viewDidLoad 方法内的 *name 数组中。

我有一个滑块可以修改该数组内的值。仅当滑块的值发生变化时才会调用 slider 方法。但是,我运行了这段代码,每次我的程序退出 viewDidLoad 方法时,我都会丢失添加到全局变量名称中的值。我可以看到它们在退出 viewDidLoad 方法之前就在那里。

我做错了什么?

编辑:内部 viewDidLoad

if (name == nil)
    name = [NSMutableArray array];
UITextField *nameTemp = [[UITextField alloc] initWithFrame:CGRectMake(20,20,20,20)];
nameTemp.returnKeyType = UIReturnKeyDone;
etc
[self.view addSubview: nameTemp];
[name addObject:nameTemp]
[nameTemp release];

At the top of my .m file I have

static NSMutableArray *name;

I load a bunch of values into my *name array inside my viewDidLoad method.

I have a slider that can modify the values inside this array. The slider method is only called when the value of the slider changes. However, I ran this code and every time my program exits the viewDidLoad method I lose the values that were added to the global variable name. I can see that they were there before exiting the viewDidLoad method.

What am I doing wrong?

EDIT: Inside viewDidLoad

if (name == nil)
    name = [NSMutableArray array];
UITextField *nameTemp = [[UITextField alloc] initWithFrame:CGRectMake(20,20,20,20)];
nameTemp.returnKeyType = UIReturnKeyDone;
etc
[self.view addSubview: nameTemp];
[name addObject:nameTemp]
[nameTemp release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧时模样 2024-12-15 07:10:34

[NSMutableArray array] 创建一个自动释放的数组,该数组显然是在 viewDidLoad 方法末尾释放的。尝试使用 [[NSMutableArray alloc] init][[NSMutableArray array] keep] 并查看这些值在 viewDidLoad 返回后是否仍然存在。

[NSMutableArray array] creates an autoreleased array that apparently is being released at the end of your viewDidLoad method. Try using [[NSMutableArray alloc] init] or [[NSMutableArray array] retain] and see if the values persist after viewDidLoad returns.

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