Load Core Data properties to textfields

Discussion in 'iOS Programming' started by jsanto, Jan 4, 2013.

  1. jsanto, Jan 4, 2013
    Last edited: Jan 4, 2013

    jsanto macrumors newbie

    Joined:
    Jan 4, 2013
    #1
    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:

    Code:
    - (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];
    
    }
     
  2. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #2
    Where does myCoreDataVarible come from?
    and how are you using fetched Results?
     
  3. jsanto thread starter macrumors newbie

    Joined:
    Jan 4, 2013
    #3
    Hello Matt

    Thanks for your answer, my core data class is definitionsData, and i created a variable of it, "myCoreDataVariable", it has another name, but i just gave it this name to be understood. I don´t understand very well your next question but i think you´re asking where is my fetched results set, so it´s a master detail app, and fetched results is automatically set by xcode in masterviewcontroller.m...I don´t know if this affect or not my problem, but i started with just one entity, and then i created another (this one), all the code related to the first one is working, but for this one it´s not, and it should be more simpler, because it´s just loading the strings to the texfields when the view appears, but my coredatavariable is always empty.
     

Share This Page