? ? 程序運行效果截圖:
1,數(shù)據(jù)的共享可以通過AppDelegate來解決
因為AppDelegate的特殊作用,它可以在其他視圖控制器中獲取AppDelegate對象,因此,將數(shù)據(jù)模型放置到AppDelegate類中,所有的頁面都相它獲取數(shù)據(jù),并且共同維護它(CRUD操作),如此就可以實現(xiàn)數(shù)據(jù)共享。
例如原型視圖控制器中的loadAllContractor方法可以修改如下:
-(void)loadAllContractor{
//獲取當(dāng)前應(yīng)用指針,訪問所有聯(lián)系人集合
AppDelegate*appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
self.allContactorsArray= appDelegate.allContactorsArray;
}
2、數(shù)據(jù)的持久化
持久化方式很多,數(shù)據(jù)庫這里先不提,而是選用簡單的plist文件來解決
步驟:
1)修改MPContactor類,實現(xiàn)NSCoding協(xié)議,讓其支持歸檔。
這里注意,處理歸檔相關(guān)操作,還新增了兩個成員屬性:favorite和portraitFileName
@property(nonatomic,assign)BOOLfavorite;
@property(nonatomic,retain)NSString*portraitFileName;
-(id)initWithName:(NSString*)name andPortraitFileName:(NSString*)fileName andIntroduce:(NSString*)introduce;
//加載plist文件,讀取聯(lián)系人信息,如果不存在則創(chuàng)建
if([selfloadAllContactorFromPlistFile] ==NO) {
[selfinitAndSaveAllContactorsToPlistFile];
}
//程序運行,從plist文件中加載數(shù)據(jù),如果成功返回YES,如果文件不存在則返回NO
-(BOOL)loadAllContactorFromPlistFile{
NSString*filePath =NSHomeDirectory();
NSString*filePath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
NSLog(@"%@",filePath);
self.allContactorsArray= [NSKeyedUnarchiverunarchiveObjectWithFile:filePath];
if(self.allContactorsArray!=nil) {
returnYES;
}
returnNO;
}
//首次讀取需要創(chuàng)建并初始化plist文件,主要步驟如下
//1、創(chuàng)建字典集合,初始化數(shù)據(jù)
//2、將當(dāng)前數(shù)據(jù)寫入plist文件
-(void) initAndSaveAllContactorsToPlistFile{
MPContactor*liwen = [[MPContactoralloc]initWithName:@"李玟"andPortraitFileName:@"liwen.png"andIntroduce:@"國際范兒的歌手,大氣、努力、態(tài)度誠懇!"];
MPContactor*likeqin = [[MPContactoralloc]initWithName:@"李克勤"andPortraitFileName:@"likeqin.png"andIntroduce:@"唱功厲害,態(tài)度認真"];
MPContactor*xujiaying = [[MPContactoralloc]initWithName:@"徐佳瑩"andPortraitFileName:@"xujiaying.png"andIntroduce:@"國際范兒的歌手,大氣、努力、態(tài)度誠懇!"];
MPContactor*zhangxinzhe = [[MPContactoralloc]initWithName:@"張信哲"andPortraitFileName:@"zhangxinzhe.png"andIntroduce:@"姜是老的辣"];
MPContactor*rongzuer = [[MPContactoralloc]initWithName:@"容祖兒"andPortraitFileName:@"rongzuer.png"andIntroduce:@"性格不錯,明明可以靠臉,但是..."];
MPContactor*laolang = [[MPContactoralloc]initWithName:@"老狼"andPortraitFileName:@"laolang.png"andIntroduce:@"時代的標志性人物,悲催的是沒來及找到感覺就..."];
MPContactor*suyunying = [[MPContactoralloc]initWithName:@"蘇運瑩"andPortraitFileName:@"suyunying.png"andIntroduce:@"有特點的丫頭,確實夠野!"];
MPContactor*huangzhilie = [[MPContactoralloc]initWithName:@"黃致列"andPortraitFileName:@"huangzhilie.png"andIntroduce:@"有實力,但是比較明顯的工業(yè)特征,還是厲害的。"];
liwen.favorite=YES;
rongzuer.favorite=YES;
xujiaying.favorite=YES;
self.allContactorsArray= [NSMutableArrayarrayWithObjects:liwen,likeqin,xujiaying,zhangxinzhe,rongzuer,laolang,suyunying,huangzhilie,nil];
[selfsaveAllContactorsDataToPlistFile];
}
3)修改個頁面的數(shù)據(jù)源初始化代碼:
-(void)loadAllContractor{
//獲取當(dāng)前應(yīng)用指針,訪問所有聯(lián)系人集合
AppDelegate*appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
self.allContactorsArray= appDelegate.allContactorsArray;
}