2013-04-08 73 views
0

我一直在試圖從tableview中傳遞一些數據到editviewcontroller,才能夠對其進行編輯並保存,但我一直沒幸運編輯CoreData值

我不知道該怎麼通過從選定行這些值的EditView中,然後保存新的值

這裏是我的代碼:

- (NSManagedObjectContext *)managedObjectContext{ 
NSManagedObjectContext *context = nil; 
id delegate = [[UIApplication sharedApplication] delegate]; 
if ([delegate performSelector:@selector(managedObjectContext)]) { 
    context = [delegate managedObjectContext]; 
} 
return context; 
} 
-(IBAction)atras:(id)sender{ 
MenuViewController *m=[[MenuViewController alloc] initWithNibName:nil bundle:nil]; 
[self presentViewController:m animated:YES completion:nil]; 

} 
-(IBAction)guardar:(id)sender{ 
NuevaTareaViewController *n=[[NuevaTareaViewController alloc] initWithNibName:nil bundle:nil]; 
[self presentViewController:n animated:YES completion:nil]; 

} 
- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 

// Fetch the devices from persistent data store 
NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Tareas"]; 
self.tareas = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

[tableView reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return self.tareas.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell; 

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


// Configure the cell... 
NSManagedObject *materia = [self.tareas objectAtIndex:indexPath.row]; 
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [materia valueForKey:@"nombre"]]]; 
cell.textLabel.textColor=[UIColor whiteColor]; 
cell.textLabel.font=[UIFont fontWithName:@"Chalkduster" size:(16)]; 

NSString *detalle=[NSString stringWithFormat:@"%@: Toca para ver los Detalles",[materia valueForKey:@"materia"]]; 
[cell.detailTextLabel setText:detalle]; 
cell.detailTextLabel.textColor=[UIColor whiteColor]; 
cell.detailTextLabel.font=[UIFont fontWithName:@"Chalkduster" size:(12)]; 

return cell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the specified item to be editable. 
return YES; 
} 


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSManagedObjectContext *context = [self managedObjectContext]; 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete object from database 
    [context deleteObject:[self.tareas objectAtIndex:indexPath.row]]; 

    NSError *error = nil; 
    if (![context save:&error]) { 
     NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
     return; 
    } 

    // Remove device from table view 
    [self.tareas removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 
} 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// Uncomment the following line to preserve selection between presentations. 
// self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

NSManagedObject *materia = [self.tareas objectAtIndex:indexPath.row]; 

NSString *perfil_activo = [materia valueForKey:@"descripcion"]; 
NSUserDefaults *defs=[NSUserDefaults standardUserDefaults]; 
//[defs setObject:perfil_activo forKey:@"perfil_activo"]; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"texto" message:perfil_activo delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; 
[a show]; 

} 

感謝 聖地亞哥

回答

0

你想獲取當前選定的對象。

NSIndex selectedRow = self.tableView.selectedRow;

使用該指數在您的實現代碼如下: NSManagedObject *selectedObject = [self.tableView objectAtIndex:selectedRow]

在你NuevaTareaViewController應該有一個屬性: NSManagoedObjectContext Tarea

如果是這樣,那麼你可以做:

-(IBAction)guardar:(id)sender { 

NSIndex selectedRow = self.tableView.selectedRow; 
NSManagedObject *selectedObject = [self.tableView objectAtIndex:selectedRow]; 

NuevaTareaViewController *n=[[NuevaTareaViewController alloc] initWithNibName:nil  bundle:nil]; 

[n setManagedObjectContext:self.context]; 
[n setTarea:selectedObject]; 

[self presentViewController:n animated:YES completion:nil]; 

}

您可以將您的託管對象上下文保存在您的NuevaTareaViewController中。

這就是說,閱讀有關NSFetchedResultsControllerStoryboard Segues,你可能會發現他們非常有幫助。