Saturday, 22 January 2011

NSUserDefaults getting erased


as part of didfinishlaunchingwithoptions method:
code:
  nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];          nsstring *firstname = [defaults objectforkey:@"firstname"];     if (firstname == nil) {         uialertview *alertforname = [[uialertview alloc] initwithtitle:@"enter first & last name"     message:@"             "                                                          delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil];            mytextfield = [[uitextfield alloc] initwithframe:cgrectmake(12, 45, 260, 25)];                  cgaffinetransform mytransform = cgaffinetransformmaketranslation(0, 60);         [alertforname settransform:mytransform];         [mytextfield setbackgroundcolor:[uicolor whitecolor]];         [alertforname addsubview:mytextfield];         [alertforname show];         [alertforname release];              }     else {         [self performselector: @selector(checkforservice)                     withobject: nil                     afterdelay: 0];      } 
the intent of check if there value entered firstname key...if value detected, runs checkforservice method declared...if not, pulls alertview textfield enter in name. clicking ok runs code:
code:
  - (void)alertview:(uialertview *)alertforname clickedbuttonatindex:(nsinteger)buttonindex { 	 	switch (buttonindex) { 		case 0: 		{	self.name = [mytextfield text];             nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];             [defaults setobject:name forkey:@"firstname"];             [defaults synchronize]; 		}             	 	 } } 
this save textfield text nsuser key firstname.

checkforservice code (to check if value of firstname key contained in text of html code) this:
code:
  - (void)checkforservice {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];          nsstring *firstname = [defaults objectforkey:@"firstname"];     if ([content rangeofstring:firstname].location != nsnotfound) {         uialertview *cancelled = [[uialertview alloc] initwithtitle:@"scheduled serve" message:@"you scheduled serve week.  please check order of worship page find assignment." delegate:self cancelbuttontitle:@"dismiss" otherbuttontitles:nil];                  [cancelled show];         [cancelled release];     } } 
here issue:
run first time, put in name. close app, close multi-tasking, , open again. time, pulls alertview says scheduled serve, because name found in document. close app , close multi-tasking bar, , run again. asking name again. causing key deleted asks again?

here problem
forgot multiple alertviews call same clickedbuttonatindex, when clicked dismiss alert lets user know have scheduled, running same code when user clicked ok after putting in name. however, since there no textfield other alertview, set nsuserdefault key right nil. so, set tags alertviews , ran default synchronize on input one.
 

there way mark thread resolved. think should that.
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment