iOS上常用四种数据存取方法有:
属性列表
对象归档
iOS的嵌入式关系数据库(SQLite3)
苹果公司提供持久性共聚Core Data
它们的应用程序都有自己的/Documents文件夹,各自的应用程序只能读写自己的/Documents目录内容
1.创建一个新工程叫PersistenceDemo; File->New->Project ->single View Application -> next
2.在PersistenceViewController.h文件声明输出口和和实例方法
#import <UIKit/UIKit.h>
@interface PersistenceViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *field1;
@property (weak, nonatomic) IBOutlet UITextField *field2;
@property (weak, nonatomic) IBOutlet UITextField *field3;
@property (weak, nonatomic) IBOutlet UITextField *field4;
-(NSString *)dataFilePath;
-(void)applicationWillResignActive:(NSNotification *)notification;
@end
3.打开 PersistenceViewController.xib文件拖4个laibel静态标签和4个TextField,然后和输出口关联(右键Fiel‘s Owner 拖到TextField上松开)
4.在对应的.m文件中
dataFilePath将kFileName串联到Documents目录的路径,以创建并返回数据文件的完整路径名
-(NSString *)dataFilePath
{
/*常量NSDocumentDirectory表明我们正在查找Documents目录路径,第二个常量NSUserDomainMask表示的是把搜索范围定在应用程序沙盒中,YES表示的是希望希望该函数能查看用户主目录*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 数组索引0处Documentd目录,
NSString *documentDirectory = [paths objectAtIndex:0];
// 返回一个kFileName的完整路径
return [documentDirectory stringByAppendingPathComponent:kFileName];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 检查数据文件是否存在,不存在则不加载
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
}
// 注册一个通知,按下home键,执行applicationWillResignActive:方法
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillTerminateNotification
object:app];
}
//当按下home键的时候调用这个方法
-(void)applicationWillResignActive:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
// 将TextField中数据写入到属性表之中
[array writeToFile:[self dataFilePath] atomically:YES];
}
5. NSNotification 和 NSNotificationCenter
Notification对象非常简单. 它就是poster要提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和 object. 一般object都是指向poster(为了让observer在接受到notification时可以回调到poster)
所以,notification有两个方法
- (NSString *)name
- (id)object
NSNotificaitonCernter是架构的大脑了.它允许我们注册observer对象, 发送notification, 撤销observer对象注册
下面是它的一些常用方法
+ (NSNotificationCenter *)defaultCenter
返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]
- (void)addObserver:(id)anObserver
selector:(SEL)aSelector
name:(NSString *)notificationName
object:(id)anObject
注册anObserver对象:接受名字为notificationName, 发送者为anObject的notification. 当anObject发送名字为notificationName的notification时, 将会调用anObserver的aSelector方法,参数为该notification. 如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer
- (void)postNotification:(NSNotification *)notification
发送notification至notification center
- (void)postNotificationName:(NSString *)aName
object:(id)anObject
创建并发送一个notification
- (void)removeObserver:(id)observer
移除observer
引用 http://blog.csdn.net/zhanglei5415/article/details/6454940
源代码:http://download.csdn.net/detail/duxinfeng2010/4445711