ios - Problems saving values in NSUserDefaults -
i have 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 viewwilldisappear
have:
-(void)viewwilldisappear:(bool)animated { string1 = [cells objectatindex:0]; [defaults setobject:string1 forkey:@"string1"]; }
but cannot data saved. know i'm doing wrong?
are setting defaults [nsuserdefaults standarduserdefaults]
somewhere? also, why comparing first object in cells array? seems strange.
you've otherwise got idea correct. make sure viewwilldisappear method getting called. can call [nsuserdefaults synchronize]
save changes immediately.
one last thing, aren't doing in else {} block. need assign value of [defaults stringforkey:@"string1"];
variable use it.
Comments
Post a Comment