1.建立单例视图
//
// ios24_saveDataViewController.h
// ios24-saveData
//
// Created by on 13-6-17.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ios24_saveDataViewController : UIViewController<UITextFieldDelegate>
{
UITextField *tfStuId;
UITextField *tfStuName;
UITextField *tfClass;
}
@property(nonatomic,retain) IBOutlet UITextField *tfStuId;
@property(nonatomic,retain) IBOutlet UITextField *tfStuName;
@property(nonatomic,retain) IBOutlet UITextField *tfClass;
-(IBAction)saveData;
-(IBAction)loadData;
@end
//
// ios24_saveDataViewController.m
// ios24-saveData
//
// Created by on 13-6-17.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import "ios24_saveDataViewController.h"
@implementation ios24_saveDataViewController
@synthesize tfClass,tfStuId,tfStuName;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
//保存
-(IBAction)saveData{
NSLog(@"save");
//设置文件保存的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.plist"];
//NSLog(@"path:%@",savePath);
//获取用户输入的内容
NSMutableDictionary *stuData = [[NSMutableDictionary alloc] init];
//添加数据到字典
[stuData setObject:tfStuId.text forKey:@"stuId"];
[stuData setObject:tfStuName.text forKey:@"stuName"];
[stuData setObject:tfClass.text forKey:@"stuClass"];
//保存到文件
[stuData writeToFile:savePath atomically:YES];
//[stuData release];
}
//加载数据
-(IBAction)loadData{
NSLog(@"load");
//设置文件保存的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取documents路径
NSString *documentPath = [paths lastObject];
//定义全路径
NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.plist"];
//从文件读取数据到字典
NSDictionary *readData = [NSDictionary dictionaryWithContentsOfFile:savePath];
NSLog(@"readData:%@",readData);
//显示出来
tfStuId.text = [readData objectForKey:@"stuId"];
tfStuName.text = [readData objectForKey:@"stuName"];
tfClass.text = [readData objectForKey:@"stuClass"];
}
//当你按了return
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
tfClass.delegate=self;
tfStuId.delegate=self;
tfStuName.delegate=self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end