2013年1月13日 星期日

Test



Hi

Hi

Hi

Hi

Hi

2011年5月20日 星期五

Using Core Data to create simple To-Do List

  1. Create project, navigation-based, named "TestCoreData"
  2. Open TestCoreData.xcdatamodeld, add atribute "name", "string" type, then add attribute "note", "string" type


  1. add file, cocoa touch -> UIViewController subclass, with nib
  2. make nib like this

  1. modify addNewController.h



//
//  addNewDataController.h
//  TestCoreData
//
//  Created by Ryan on 2011/5/19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface addNewDataController : UIViewController {

IBOutlet UITextField *myName;
IBOutlet UITextField *myNote;
}
- (IBAction)toSave:(id)sender;
- (IBAction)toCancel:(id)sender;

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;


@end
  1. modify insertNewObject method in RootViewController.m


- (void)insertNewObject
{

addNewDataController  *viewController=[[addNewDataController alloc]initWithNibName:@"addNewDataController" bundle:nil];

viewController.fetchedResultsController=self.fetchedResultsController;

[self presentModalViewController:viewController animated:YES];
[viewController release];
}
  1. modify fetchResultsController in RootViewController.m, timestamp -> name


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
  1. modify toSave and toCancel method in addNewDataController.m


- (IBAction)toSave:(id)sender {

// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];


// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:myName.text forKey:@"name"];
[newManagedObject setValue:myNote.text forKey:@"note"];


// Save the context.
NSError *error = nil;
if (![context save:&error])
{
    /*
     Replace this implementation with code to handle the error appropriately.
 
     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}


[self toCancel:nil];

}

- (IBAction)toCancel:(id)sender {


[self.parentViewController dismissModalViewControllerAnimated:YES];

}
  1. try it!


PS. If you can't see the gray note text, go to RootViewController.m, in cellForRowAtIndexPath

if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}