您好,登錄后才能下訂單哦!
iOS上常用四種數(shù)據(jù)存取方法有:
屬性列表
對(duì)象歸檔
iOS的嵌入式關(guān)系數(shù)據(jù)庫(kù)(SQLite3)
蘋(píng)果公司提供持久性共聚Core Data
它們的應(yīng)用程序都有自己的/Documents文件夾,各自的應(yīng)用程序只能讀寫(xiě)自己的/Documents目錄內(nèi)容
1.創(chuàng)建一個(gè)新工程叫PersistenceDemo; File->New->Project ->single View Application -> next
2.在PersistenceViewController.h文件聲明輸出口和和實(shí)例方法
#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
4.在對(duì)應(yīng)的.m文件中
dataFilePath將kFileName串聯(lián)到Documents目錄的路徑,以創(chuàng)建并返回?cái)?shù)據(jù)文件的完整路徑名
-(NSString *)dataFilePath { /*常量NSDocumentDirectory表明我們正在查找Documents目錄路徑,第二個(gè)常量NSUserDomainMask表示的是把搜索范圍定在應(yīng)用程序沙盒中,YES表示的是希望希望該函數(shù)能查看用戶主目錄*/ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // 數(shù)組索引0處Documentd目錄, NSString *documentDirectory = [paths objectAtIndex:0]; // 返回一個(gè)kFileName的完整路徑 return [documentDirectory stringByAppendingPathComponent:kFileName]; }
- (void)viewDidLoad { [super viewDidLoad]; // 檢查數(shù)據(jù)文件是否存在,不存在則不加載 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]; } // 注冊(cè)一個(gè)通知,按下home鍵,執(zhí)行applicationWillResignActive:方法 UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillTerminateNotification object:app]; }
//當(dāng)按下home鍵的時(shí)候調(diào)用這個(gè)方法 -(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中數(shù)據(jù)寫(xiě)入到屬性表之中 [array writeToFile:[self dataFilePath] atomically:YES]; }
Notification對(duì)象非常簡(jiǎn)單. 它就是poster要提供給observer的信息包裹. notification對(duì)象有兩個(gè)重要的成員變量: name 和 object. 一般object都是指向poster(為了讓observer在接受到notification時(shí)可以回調(diào)到poster)
所以,notification有兩個(gè)方法
- (NSString *)name
- (id)object
NSNotificaitonCernter是架構(gòu)的大腦了.它允許我們注冊(cè)observer對(duì)象, 發(fā)送notification, 撤銷observer對(duì)象注冊(cè)
下面是它的一些常用方法
+ (NSNotificationCenter *)defaultCenter
返回notification center [類方法,返回全局對(duì)象, 單件模式.cocoa的很多的全局對(duì)象都是通過(guò)類似方法實(shí)現(xiàn)]
- (void)addObserver:(id)anObserver
selector:(SEL)aSelector
name:(NSString *)notificationName
object:(id)anObject
注冊(cè)anObserver對(duì)象:接受名字為notificationName, 發(fā)送者為anObject的notification. 當(dāng)anObject發(fā)送名字為notificationName的notification時(shí), 將會(huì)調(diào)用anObserver的aSelector方法,參數(shù)為該notification. 如果notificationName為nil. 那么notification center將anObject發(fā)送的所有notification轉(zhuǎn)發(fā)給observer
. 如果anObject為nil.那么notification center將所有名字為notificationName的notification轉(zhuǎn)發(fā)給observer
- (void)postNotification:(NSNotification *)notification
發(fā)送notification至notification center
- (void)postNotificationName:(NSString *)aName
object:(id)anObject
創(chuàng)建并發(fā)送一個(gè)notification
- (void)removeObserver:(id)observer
移除observer
引用 http://blog.csdn.net/zhanglei5415/article/details/6454940
源代碼:http://download.csdn.net/detail/duxinfeng2010/4445711
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。