OBJ. C. (XCODE 5 - IOS 7) Auto save variables in a property list

214 views Asked by At

I want to save automatically my variables (floats) in a property list that I've already create. I can do that with a button, that work good but I want to do that automatically WITH IOS 7 (some methods are deprecated in IOS 6 or 7).

replyToApplicationShouldTerminate 

doesn't work...

I save in the property list with a button like that:

-(IBAction)apply:(id)sender
{
  {

    [nameC1 resignFirstResponder];
    [nameC2 resignFirstResponder];
    [nameC3 resignFirstResponder];
    [nameC4 resignFirstResponder];
    [nameC5 resignFirstResponder];


    nom1 = [nameC1 text];
    nom2 = [nameC2 text];
    nom3 = [nameC3 text];
    nom4 = [nameC4 text];
    nom5 = [nameC5 text];

    v1 = [NSString stringWithFormat:@"%d", compteur1];
    v2 = [NSString stringWithFormat:@"%d", compteur2];
    v3 = [NSString stringWithFormat:@"%d", compteur3];
    v4 = [NSString stringWithFormat:@"%d", compteur4];
    v5 = [NSString stringWithFormat:@"%d", compteur5];



    //Write in Data.plist
    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                               [NSArray arrayWithObjects: nom1, nom2, nom3, nom4, nom5, v1, v2, v3, v4, v5, nil] forKeys:[NSArray arrayWithObjects: @"nomCompteur1", @"nomCompteur2", @"nomCompteur3", @"nomCompteur4", @"nomCompteur5", @"valeurCompteur1", @"valeurCompteur2", @"valeurCompteur3", @"valeurCompteur4", @"valeurCompteur5",nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        NSLog(@"error");
        //[error release];
    }


 }

}
1

There are 1 answers

0
Abhi Beckert On BEST ANSWER

In after the nib is loaded, register a notification for value changes in each of your text fields:

- (void)awakeFromNib
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(UITextFieldTextDidChange:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:nameC1];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(UITextFieldTextDidChange:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:nameC2];
  ...
}

And when the text changes, save the data:

- (void)UITextFieldTextDidChange:(NSNotification*)notification
{
  [self save];
}

- (void)save
{
   ...
}

You might want to get a bit more fancy, and use dispatch_after() to wait ~0.5 seconds before doing the save. That way the user won't notice if the save is a bit slow. But I suspect the save will be very fast in this case.