I use this 3 methods to enter text i enter on textfields, and after entering data in textfields press home button on simulator and then quit application and when again launch application so textfield was again empty,means data is not entered in plist?/ y is it so ,??? m not making any plist file manually,whatever did is just in this 3 methods
1)
1)
Code:
-(NSString *)dataFilePath
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
2)
-(void) applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array=[[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[[array description] writeToFile:[self dataFilePath] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
// [array release];
}
3)
- (void)viewDidLoad
{
NSString *filePath=[self dataFilePath];
NSLog(@"%@",filePath);
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array=[[NSArray alloc] initWithContentsOfFile:filePath];
field1.text=[array objectAtIndex:0];
field2.text=[array objectAtIndex:1];
field3.text=[array objectAtIndex:2];
field4.text=[array objectAtIndex:3];
[array release];
}
UIApplication *app=[UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}
Last edited by a moderator: