简体   繁体   中英

Global array loses values after exiting method

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];

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM