How would I save and load a UITextField?

前端 未结 3 545
死守一世寂寞
死守一世寂寞 2021-01-26 10:04

I have searched everywhere and tried lots of code but nothing seems to be working for me. All I need to do is to load (on viewDidLoad) a text field and save it when a button is

3条回答
  •  故里飘歌
    2021-01-26 10:59

    Create a NSMutableDictionary as property and ...

    when your button is clicked:

    -(IBAction)buttonClicked:(id)sender
    {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];   
    NSString *devicePath = [documentsDirectory stringByAppendingPathComponent:@"yourfile.txt"]; 
    [self.dictionary setObject:self.textField.Text key:@"textField"];
    [self.dictionary writeToFile:devicePath atomically:YES];
    }
    

    On your viewDidLoad, you can get the value of the file by:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youefile"
                                                         ofType:@"txt"];
    self.dictioary = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] autorelease];
    

提交回复
热议问题