I´m having trouble finding the proper way to load my saved core data to my textfields. I have a view with static cells and each cell has multiple textfields, nothing especial, the idea is just to fill the textfields and hit the button save, and every time the user goes to this view, the textfields appear with the last imputed text.
Everything works great until the save part, the problem is when switching to another view and then come back, the textfields are not filled. I know i have to work with view did load and fetch the data, but i don´t know what i´m doing wrong:
- (void)viewDidLoad
{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.managedObjectContext=[appDelegate managedObjectContext];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[self fetchedResultsController];
self.myTextfield.text = myCoreDataVariable.coreDataProperty;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequestDefinitions = [[NSFetchRequest alloc] init];
NSEntityDescription *entitydefinitionsData = [NSEntityDescription entityForName:@"DefinitionsData" inManagedObjectContext:self.managedObjectContext];
[fetchRequestDefinitions setEntity:entitydefinitionsData];
return _fetchedResultsController;
}
- (IBAction)Guardar:(id)sender
{
if (myCoreDataVariable == NULL)
{
myCoreDataVariable = [NSEntityDescription insertNewObjectForEntityForName:@"DefinitionsData" inManagedObjectContext:self.managedObjectContext];
}
myCoreDataVariable.coreDataProperty = self.myTextfield.text;
[self.managedObjectContext save:Nil];
}