iOS培训之数据保持(五)

作者:达内 更新时间:2013-12-30 11:46 来源:未知 点击:
应用程序委托: 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 2 { 3 // Override point for customization after application launch。
应用程序委托:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)
 
launchOptions
2 {
3     // Override point for customization after application launch.
4     NSDictionary *defaults=[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber 
 
numberWithBool:YES],kWarpDriveKey,[NSNumber numberWithInt:5],kWarpFactorKey,@"Greed",kFavoriteSinKey, 
 
nil];
5     [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
6     return YES;
7 }//第一次运行程序时,对设置束赋默认初值。 
 视图控制器:
1     NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
2     usernameLabel.text=[defaults objectForKey:kUsernameKey];
3     //读取方法,利用NSUserDefaults的单例方法。键值为设置束中的每项的Identifier。
4  
5     NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
6     [defaults setBool:engineSwitch.on forKey:kWarpDriveKey];
7     //保存方法 
在Settings中可设置应用程序,在应用程序中亦可设置反馈给Settings。Documents文件夹中没有生成数据保存文件。
6.UIDocument管理文档存储
模型类:
先建立作为UIDocument子类的数据模型类,在类里实现以下UIDocument方法和其它模型方法
 1 -(id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
 2 {
 3     NSLog(@"saving document to URL %@",self.fileURL);//输出保存的路径
 4     return [bitmap copy];//bitmap为保存的mutabledata数据
 5 }//保存
 6  
 7 -(BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)
 
outError
 8 {
 9     NSLog(@"loading document from URL %@",self.fileURL);
10     self.bitmap =[contents copy];
11     return true;
12 }//加载 
 控制器: 
读取文档路径集
1 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
2 NSString *path=[paths objectAtIndex:0];
3 NSFileManager *fm=[NSFileManager defaultManager];
4 NSError *dirError;
5 NSArray *files=[fm contentsOfDirectoryAtPath:path error:&dirError];
6 //数组内排序
7 self.documentFileNames=files; 
 读取文档URL路径 
1 -(NSURL *)urlForFilename:(NSString *)filename
2 {
3     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
4     NSString *documentDirectory=[paths objectAtIndex:0];
5     NSString *filePath=[documentDirectory stringByAppendingPathComponent:filename];
6     NSURL *url=[NSURL fileURLWithPath:filePath];
7     return url;
8 } 
 创建文档,并设置保存 
 1         NSString *filename=[NSString stringWithFormat:@"%@.tinypix",[alertView 
 
textFieldAtIndex:0].text];
 2         NSURL *saveUrl=[self urlForFilename:filename];
 3         self.chooseDocument=[[BIDTinyPixDocument alloc] initWithFileURL:saveUrl];//创建UIDocument子类
 
实例对象
 4         [chooseDocument saveToURL:saveUrl forSaveOperation:UIDocumentSaveForCreating 
 
completionHandler:^(BOOL success) {
 5             if(success)
 6             {
 7                 NSLog(@"save OK");
 8                 //addition
 9             }
10             else
11                 NSLog(@"failed to save!");
12          }]; 
 打开文档 
 1         self.chooseDocument=[[BIDTinyPixDocument alloc] initWithFileURL:docUrl];
 2         [self.chooseDocument openWithCompletionHandler:^(BOOL success) {
 3             if(success)
 4             {
 5                 NSLog(@"load OK");
 6                 //addition
 7             }
 8             else
 9                 NSLog(@"failed to load!");
10         }]; 
 关闭文档(保持编辑数据) 
1 UIDocument *doc=self.chooseDocument;
2 [doc closeWithCompletionHandler:nil]; 
沙盒中的Documents文件夹生成filename.tinypix文件。 
每一件事都要用多方面的角度来看它,学习也同样如此,碰到问题要从多方面去考虑,达内会在IOS学习的道路上指导大家如何更好更快速的学习IOS。
标签:

相关阅读

最新开班信息

3G-IOS软件工程师就业班
北京潘家园中心 开课日期:9月29日
3G-IOS软件工程师就业班
北京潘家园中心 开课日期:9月29日
3G-IOS软件工程师周末班
北京潘家园中心 开课日期:9月29日
3G-IOS软件工程师周末班
北京潘家园中心 开课日期:9月29日