退出方法后全局数组丢失值
在 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[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 yourviewDidLoad
method. Try using[[NSMutableArray alloc] init]
or[[NSMutableArray array] retain]
and see if the values persist afterviewDidLoad
returns.