I have this as my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
if (defaults == [cells objectAtIndex:0])
{
NSString * myFile = [[NSBundle mainBundle]pathForResource:@"cells" ofType:@"plist"];
cells = [[NSMutableArray alloc]initWithContentsOfFile:myFile];
}
else
{
[defaults stringForKey:@"string1"];
}
}
And for my viewWillDisappear I have:
-(void)viewWillDisappear:(BOOL)animated
{
string1 = [cells objectAtIndex:0];
[defaults setObject:string1 forKey:@"string1"];
}
But I cannot get the data to be saved. Does anyone know what I’m doing wrong?
Are you setting defaults to
[NSUserDefaults standardUserDefaults]somewhere? Also, why are you comparing it to the first object in the cells array? Seems strange.You’ve otherwise got the idea correct. Make sure the viewWillDisappear method is actually getting called. Also you can call
[NSUserDefaults synchronize]to save the changes immediately.One last thing, you aren’t doing anything in that else {} block. You need to assign the value of
[defaults stringForKey:@"string1"];to a variable to use it.