![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
@interface ViewController () #define FileName @"Student.plist" #define KName @"KName" #define KBirth @"KBirth" #define KSex @"KSex" #define KNumber @"KNumber" #define KHome @"KHome" @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; NSDictionary *dictionary=[NSDictionary dictionaryWithContentsOfFile:[self filePath]]; if (dictionary) { self.nameField.text=[dictionary objectForKey:KName]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)save:(id)sender { do { NSString *name=[self.nameField text]; NSString *bith=[self.birthField text]; NSInteger sex=[self.sexSeg selectedSegmentIndex]; NSString *home=[self.homeField text]; NSString *number=[self.numberField text]; if (!name||!bith||!home||!number) { break; } NSMutableDictionary *dic=[NSMutableDictionary dictionary]; [dic setObject:name forKey:KName]; [dic setObject:bith forKey:KBirth]; [dic setObject:[NSNumber numberWithInteger:sex] forKey:KSex]; [dic setObject:number forKey:KNumber]; [dic setObject:home forKey:KHome]; if ([dic writeToFile:[self filePath] atomically:YES]) { UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"保存成功!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; } return; } while (0); UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"警告" message:@"基本信息不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; } //返回沙盒路径 - (NSString *)filePath { NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path= [[array objectAtIndex:0] stringByAppendingPathComponent:FileName]; return path; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.nameField resignFirstResponder]; [self.birthField resignFirstResponder]; [self.numberField resignFirstResponder]; [self.homeField resignFirstResponder]; } - (void)KeyboardWillShow { [UIView beginAnimations:@"show" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; CGRect rect=self.view.frame; rect.origin.y=-40; self.view.frame=rect; [UIView commitAnimations]; } - (void)KeyboardWillHide { [UIView beginAnimations:@"hide" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; CGRect rect=self.view.frame; rect.origin.y=0; self.view.frame=rect; [UIView commitAnimations]; }