0

iPad app fans:

I've got a modalviewcontroller designed to popup on a button tap so users can add notes or change data inputs. A table in the main window needs to be refreshed as changes to the database are made. The database incorporates a kind of advanced bookmark. I can create and delete database entries based on where users browse - no problem: I get the console report "saveContext KQVC line 203" and entries are quickly added into the table.

The modalviewcontroller even pops (on tapping the blue (>) detail icon also in each cell) with populated data of existing records. However the save button on the modalviewcontroller just doesn't save. (the delete function works great). Here's my code for the modalviewcontroller save button:

#import "PatternViewController.h"
#import "KnittingQueenViewController.h"

 @implementation PatternViewController
 @dynamic patternName, patternNotes, patternUrl, dateAdded;
 @synthesize patternA, patternView, knittingQueenViewController;

-(IBAction)save {
    [patternA setValue:patternName.text forKey:@"patternName"];
    [patternA setValue:patternUrl.text forKey:@"patternUrl"];
    [patternA setValue:patternNotes.text forKey:@"patternNotes"];
    dateAdded = [NSDate date];
    [patternA setValue:dateAdded forKey:@"dateAdded"];
    [patternA setValue:patternLabel.text forKey:@"patternLabel"];
    [patternA setValue:pngPath forKey:@"patternPhoto"];   

    [knittingQueenViewController saveContext];
    [self dismissModalViewControllerAnimated:YES];
}

and the saveContext method in the knittingqueenviewcontroller:

- (void)saveContext {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
    NSError *error = nil;
    if (![context save:&error]) {
         // error code goes in here
    }
    NSLog(@"saveContext KQVC line 203");
    [self refreshPatternsTable];
}

I get no errors, but no saved data either. My question: why isn't the method saveContext being called from the modalviewcontroller? Any suggestions you can offer would be most appreciated.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Peter Brockmann
  • 3,594
  • 3
  • 28
  • 28

1 Answers1

3

Is your save method being called? Is knittingQueenViewController non-nil when it's called? Do the values of patternA look correct?

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • One of the first two questions here is almost certainly the issue. – Seamus Campbell Aug 18 '11 at 04:18
  • @noa Answer to Q1: the method was being called but because the knittingQueenViewController was nil http://stackoverflow.com/questions/3678180/how-to-check-if-a-specific-uiviewcontrollers-view-is-currently-visible tested per this answer. Answer to Q2: it was nil. So I've alloc init and now it crashes. – Peter Brockmann Aug 18 '11 at 04:35
  • Allocating another one here won't help you – it doesn't have a references to your FetchedResultsController or ManagedObjectContext, for example. Where do you first allocate and initialize PatternViewController? From code? You probably need to add some code there that sets `knittingQueenViewController` to the already-created instance. – paulmelnikow Aug 18 '11 at 04:40
  • @noa Yes, it's from code. Here's the allocate and initialize of `PatternViewController` in `KnittingQueenViewController`. `-(IBAction)newPattern { PatternViewController *controller = [[PatternViewController alloc] initWithNibName:@"PatternViewController" bundle:nil]; controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; controller.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:controller animated:YES]; [controller release]; }` What would you suggest? – Peter Brockmann Aug 18 '11 at 04:44
  • Cool. Just add `controller.knittingQueenViewController = self;` after the call to `initWithNibName`. – paulmelnikow Aug 18 '11 at 04:46
  • @noa nice. Method `saveContext` now gets called, but there's still no change to the table. Modifications aren't being committed... – Peter Brockmann Aug 18 '11 at 04:51
  • How about Q3 / values of patternA in save? – paulmelnikow Aug 18 '11 at 05:08
  • @noa Now, we can edit and delete existing table entries using the `PatternViewController` ModalViewController. YEAH. But, creating a new entry from the `PatternViewController` goes through the `saveContext` but doesn't get added to the table. :( Suggestions? – Peter Brockmann Aug 18 '11 at 15:18
  • @noa - works now. Thanks! I needed to add the reference to ` **knittingQueenViewController.**` in the lines for each item in the `[knittingQueenViewController.patternA setValue:patternName.text forKey:@"patternName"];` statement in the `save` method of the `PatternViewController`. – Peter Brockmann Aug 18 '11 at 18:38