使用plist

-(NSString *)dataFilePath//用來返回數據文件的完整路徑名
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *paths = [path objectAtIndex:0];
    return [paths stringByAppendingPathComponent:kFileName];
    
}

-(void)applicationWillTerminate:(NSNotification *)notification//應用程序在退出時調用,將數據保存到數據列表
{
    NSMutableArray *array = [[
                              NSMutableArray alloc]init];
    [array addObject:[text_id text]];
    [array addObject:[text_password text]];
    [array writeToFile:[self dataFilePath] atomically:YES];
    //[array release];
}

-(void)viewDidLoad
{
    NSString *filePath = [self dataFilePath];//得到文件路徑
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])//如果該文件存在就加載
    {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        [text_id setText:[array objectAtIndex:0]];
        [text_password setText:[array objectAtIndex:1]];     //[array release];
    }
    
    
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)
                                                 name:UIApplicationWillTerminateNotification
                                               object:app];
    [super viewDidLoad];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章