iOS培训之数据保持(四)

作者:达内 更新时间:2013-12-30 11:45 来源:未知 点击:
根视图控制器: 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view from its nib. 5 6 BIDAppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
根视图控制器:
 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view from its nib.
 5     
 6     BIDAppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
 7     NSManagedObjectContext *context=[appDelegate managedObjectContext];
 8     NSEntityDescription *entityDecription=[NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];//关联实体与上下文
 9     NSFetchRequest *request=[[NSFetchRequest alloc] init];
10     [request setEntity:entityDecription];//设置抓取实体
11     NSError *error;
12     /*因为要抓取实体中的所有项,所以没有设置抓取属性*/
13     NSArray *objects=[context executeFetchRequest:request error:&error];//从实体中抓取到上下文,且上下文进行跟踪对象
14     if(objects==nil)
15         NSLog(@"There was an error!");
16     for (NSManagedObject *object in objects)
17     {
18         NSNumber *lineNum=[object valueForKey:@"lineNum"]; //读取抓取出的第n个对象的lineNum属性
19         NSString *lineText=[object valueForKey:@"lineText"];
20         
21         NSString *fieldname=[[NSString alloc] initWithFormat: @"line%d",[lineNum integerValue]];
22         UITextField *field=[self valueForKey:fieldname];
23         field.text=lineText;
24     }
25     
26     UIApplication *app=[UIApplication sharedApplication];
27     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
28 }
29  
30 -(void)applicationWillResignActive:(NSNotification *)notification
31 {
32     BIDAppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
33     NSManagedObjectContext *context=[appDelegate managedObjectContext];
34     NSError *error;
35     
36     for (int i=1; i<=4; i++) {
37         NSString *fieldname=[[NSString alloc] initWithFormat:@"line%d",i];
38         UITextField *field=[self valueForKey:fieldname];
39         
40         NSFetchRequest *request=[[NSFetchRequest alloc] init];
41         
42         NSEntityDescription *entityDescritption=[NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
43         [request setEntity:entityDescritption];
44         
45         NSPredicate *predicate=[NSPredicate predicateWithFormat:@"(lineNum=%d)",i];
46         [request setPredicate:predicate];
47         
48         NSArray *objects=[context executeFetchRequest:request error:&error];//抓取上下文中的托管对象集
49         NSManagedObject *theLine=nil;
50         
51         if(objects==nil)
52             NSLog(@"There was an error!");
53         if([objects count]>0)
54         {
55             /*因设置了抓取属性,每次按抓取属性在实体抓取的上下文中的托管对象集里都只有一个对应的托管对象(在此例对应抓取属性的只有一个)*/
56             theLine=[objects objectAtIndex:0];//取托管对象
57         }
58         else
59         {
60             /*在实体中插入新的托管对象,且返回该对象和放入上下文中跟踪*/
61             theLine=[NSEntityDescription insertNewObjectForEntityForName:@"Line" inManagedObjectContext:context];
62         }
63         [theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];
64         [theLine setValue:field.text forKey:@"lineText"];
65     }
66     [context save:&error];//跟踪结束,保存进实体
67 } 
 应用程序委托: 
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 2 {
 3     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 4     // Override point for customization after application launch.
 5     self.rootcontroller=[[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
 6     UIView *rootView=self.rootcontroller.view;
 7     CGRect viewFrame=rootView.frame;
 8     viewFrame.origin.y+=[UIApplication sharedApplication].statusBarFrame.size.height;
 9     rootView.frame=viewFrame;
10     [self.window addSubview:rootView];
11     self.window.backgroundColor = [UIColor whiteColor];
12     [self.window makeKeyAndVisible];
13     return YES;
14 } 
沙盒中的Documents文件夹生成Core_Data_Persistence.sqlite文件(未知直读方法)。 
其它例子
5.应用程序设置
程序使用该方法保持的数据,可在iphone的settings中查看和设置。
添加setting.bundle,在root.plist中配置好要保存的数据项和settings中显示的分组界面。
标签:

相关阅读

最新开班信息

3G-IOS软件工程师就业班
中关村校区上课 开课日期:6月30日
3G-IOS软件工程师就业班
中关村校区上课 开课日期:9月29日
3G-IOS软件工程师周末班
中关村校区上课 开课日期:6月30日
3G-IOS软件工程师周末班
中关村校区上课 开课日期:9月29日