//前面有部分参考别人的代码
-(NSString *)dataFilePath
{
/* 常量 NSDocumentDirectory 表明我们正在查找 Documents 目录路径,第二个常量 NSUserDomainMask 表示的是把搜索范围定在应用程序沙盒中, YES 表示的是希望希望该函数能查看用户主目录 */
NSArray *paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES );
// 数组索引 0 处 Documentd 目录,
NSString *documentDirectory = [paths objectAtIndex : 0 ];
// 返回一个 kFileName 的完整路径
return [documentDirectory stringByAppendingPathComponent : @"mylist.plist" ]; // 写自己喜欢的名字
}
- ( void )viewDidLoad
{
[ super viewDidLoad ];
// 检查数据文件是否存在,不存在则不加载
}
- ( void )didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
- ( IBAction )reshai:( id )sender {
[ IDtest resignFirstResponder ];
}
- ( IBAction )res:( id )sender {
[ NameText resignFirstResponder ];
}
- ( IBAction )save:( id )sender {
NSString *filePath = [ self dataFilePath ]; // 调用前面的方法
NSMutableArray *array=[[ NSMutableArray alloc ] init ];
[array addObject : IDtest . text ];
[array addObject : NameText . text ];
[array writeToFile :filePath atomically : YES ];
}
- ( IBAction )load:( id )sender {
NSString *filePath = [ self dataFilePath ];
if ([[ NSFileManager defaultManager ] fileExistsAtPath :filePath]) {
NSArray *array = [[ NSArray alloc ] initWithContentsOfFile :filePath];
IDtest . text = [array objectAtIndex : 0 ];
NameText . text = [array objectAtIndex : 1 ];
}
// 注册一个通知,按下 home 键,执行 applicationWillResignActive: 方法
UIApplication *app = [ UIApplication sharedApplication ];
[[ NSNotificationCenter defaultCenter ] addObserver : self
selector : @selector (applicationWillResignActive:)
name : UIApplicationWillTerminateNotification
object :app];
}
@end