Hi folks

Well, I have found a very simple way to avoid secondary MOCs or new objects 
appearing in the table view, whilst adding new objects.

My example is to use a modal sheet to allow the editing of a new object.

In the NIB for the modal sheet, add a NSObjectController, set it to Class mode, 
leave the Class Name at NSMutableDictionary and check Prepares Content.

In the controller class, add the following method to respond to the "+" button 
being pressed on the main form:

- (IBAction) create:(id)sender
{
  if (panel == nil)
  {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    
    NSNib *nib = [[[NSNib alloc] initWithNibNamed:@"NewWordSheet" 
bundle:bundle] autorelease];
    
    BOOL success = [nib instantiateNibWithOwner:self topLevelObjects:nil];
    
    if (!success)
    {
      // should present error
      return;
    }
  }
  else
  {
    [self.panel makeFirstResponder:self.panel];
    
    [self.wordController setContent:[NSMutableDictionary dictionary]];
  }
    
  [NSApp beginSheet:self.panel
     modalForWindow:self.parentWindow
      modalDelegate:self
     didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
        contextInfo:NULL];
}

Then, add the sheet closure delegate:

- (void) sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode 
contextInfo:(void *)contextInfo
{
  // we are only interested in doing something with the edited word
  // if the Save button was pressed
  if (returnCode == NSOKButton)
  {
    [[self.managedObjectContext undoManager] beginUndoGrouping];

    @try
    {
      [[self.managedObjectContext undoManager] setActionName:@"Add Word"];
      
      Word *newWord = [NSEntityDescription 
insertNewObjectForEntityForName:@"Word"
                                                    
inManagedObjectContext:self.managedObjectContext];
      
      NSDictionary *editedValues = [wordController content];
      
      for (NSString *key in editedValues)
      {
        id value = [editedValues valueForKey:key];
        
        [newWord setValue:value forKey:key];
      }
      
      NSError *error;
      
      if (![self.managedObjectContext save:&error])
      {
        NSLog(@"Could not save new Word");
      }
    }
    @finally
    {
      [[self.managedObjectContext undoManager] endUndoGrouping];
    }    

    [sourceListController fetch:nil];
  }

  // close the dialog
  [sheet orderOut:self];

  // clear temporary object from controller
  [wordController setContent:nil];
}

Of course, this doesn't account for 1..N relationships, but it certainly works 
with N..1.

Joanna

--
Joanna Carter
Carter Consulting

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to