2012-07-28 3 views
0

나는 인 2 개의 속성 (ID 및 이름)이있는 핵심 데이터 파일을 가지고 있습니다.핵심 데이터 간단한 가져 오기 및 업데이트

누군가이 두 속성에서 값을 가져 와서 업데이트 할 수있는 방법을 제공 할 수 있습니까? 나는 시도하고 실패했다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCategory:)]; 
    self.navigationItem.rightBarButtonItem = addButtonItem; 

} 

-(IBAction)addCategory:(id)sender 
{ 
    UIAlertView *addCategoryAlertView = [[UIAlertView alloc] initWithTitle:@"Add Category" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 
    addCategoryAlertView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    self.textField = [addCategoryAlertView textFieldAtIndex:0]; 
    self.textField.placeholder = @"Enter category name"; 
    [addCategoryAlertView show]; 
} 

과 UIAlertView 위임 방법에

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

내가 그 범주에 값을 추가해야합니다

여기에 내 현재 코드입니다.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if (buttonIndex == kAddCategoryButtonIndex) { 
     AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
     NSError *error; 

     //NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context]; 
     //[fetchRequest setEntity:entity]; 
     [entity setValue:self.textField.text forKey:@"name"]; 
     NSLog(@"%@",[entity valueForKey:@"name"]); 
     [context save:&error]; 
    } 
} 

을 그리고 나는 그것이 작동 생각하지만, 나는이 두 속성의 값을 얻을 수 없습니다 여기

내가 지금했던 방법이다.

누군가 나를 도울 수 있으면 좋을 것입니다. 감사.

답변

0

방법

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context 

반환 NSManagedObject하지 NSEntityDescription.

그래서,이

NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context]; 
[managedObject setValue:self.textField.text forKey:@"name"]; 

할 당신이 가서 잘되어야합니다.

관련 문제