NSUserDefaults getting erased

Discussion in 'iOS Programming' started by newtoiphonesdk, Jan 28, 2012.

  1. newtoiphonesdk, Jan 28, 2012
    Last edited: Jan 28, 2012

    newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    As part of didfinishlaunchingwithoptions method:
    Code:
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        NSString *firstName = [defaults objectForKey:@"firstName"];
        if (firstName == nil) {
            UIAlertView *alertforname = [[UIAlertView alloc] initWithTitle:@"Enter Your 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 this is to check if there is already a value entered for firstName key...if a value is detected, it runs checkforservice method I declared...if not, it pulls up the alertview with textfield to enter in their name. Clicking Ok runs this 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 is to save the textfield text to the nsuser key firstName.

    The checkforservice code (to check if the value of the firstName key is contained in text of html code) is 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 To Serve" message:@"You are scheduled to serve this week.  Please check the order of worship page to find your assignment." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            
            [cancelled show];
            [cancelled release];
        }
    }
    
    Here is my issue:
    I run it the first time, I put in my name. I close the app, close it from multi-tasking, and open it again. This time, it pulls up the alertview that says I am scheduled to serve, because my name was found in the document. I close the app and close it from multi-tasking bar, and then run it again. Now it is asking for my name again. What is causing the key to be deleted that it asks again?

    Here was the problem
    Forgot that multiple alertviews would call the same clickedButtonAtIndex, so when I clicked Dismiss for the alert that lets the user know they have something scheduled, it was running the same code as when the user clicked Ok after putting in their name. However, since there is no textField for the other alertview, it would set the nsuserdefault for that key right back to nil. So, I set tags for the alertViews and only ran the default synchronize on the input one.
     
  2. xStep, Jan 28, 2012
    Last edited: Jan 28, 2012

    xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    There is a way to mark your thread as Resolved. I think you should that.
     

Share This Page