下面就来做一个简单的demo:
右击文本框,发现他的Did End On Exit事件,然后拖放到.h文件中,创建一个click事件
- (IBAction)click:(id)sender {
[senderresignFirstResponder];
}
点击mac系统上最上面一行的前往功能,然后按住alt键会显示出资源文件,该文件一般是隐藏的,前往->资源库->Application Support->iPhone Simulator->6.1->Applications->
选择自定义的文件->Library或者Documents(存储大数据,自定义文件名的文件都在这里面)
//获取沙盒文件路径
-(NSString *)getPath
{
//用来获得Document地址
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//注意:这里是NSDocument不是NSDocumentation,特别注意
NSLog(@"%@",arr);
//在地址上增加文件
NSString *path = [arr[0] stringByAppendingPathComponent:@"abc.plist"];
NSLog(@"%@",path);
return path;
//创建自定义文件,并存储或者获取文件中数据
- (void)viewDidLoad
[super viewDidLoad];
NSArray *arr = @[@"aaa",@"bbb",@"ccc"];
[arr writeToFile:[self getPath] atomically:YES];
//判断是否有文件
if([[NSFileManager defaultManager] fileExistsAtPath:[self getPath]])
{
arr = [NSArray arrayWithContentsOfFile:[self getPath]];
NSLog(@"%@",arr);
}
//获取应用
UIApplication *app = [UIApplication sharedApplication];
//在通知中心添加一个观察者,当符合UIApplicationWillResignActiveNotification条件时,调用方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(save:) name:UIApplicationWillResignActiveNotification object:app];
-(void)save:(id)sender
NSArray *arr = @[self.text1.text1,self.text2.text];
本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366423,如需转载请自行联系原作者