• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to hide new objects in a NSTableView?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to hide new objects in a NSTableView?


  • Subject: Re: How to hide new objects in a NSTableView?
  • From: Joanna Carter <email@hidden>
  • Date: Wed, 24 Mar 2010 18:02:21 +0000

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to hide new objects in a NSTableView?
      • From: Jerry Krinock <email@hidden>
References: 
 >How to hide new objects in a NSTableView? (From: Joanna Carter <email@hidden>)
 >Re: How to hide new objects in a NSTableView? (From: Kyle Sluder <email@hidden>)
 >Re: How to hide new objects in a NSTableView? (From: Joanna Carter <email@hidden>)
 >Re: How to hide new objects in a NSTableView? (From: Jerry Krinock <email@hidden>)

  • Prev by Date: Re: code folding in NSTextView
  • Next by Date: Re: code folding in NSTextView
  • Previous by thread: Re: How to hide new objects in a NSTableView?
  • Next by thread: Re: How to hide new objects in a NSTableView?
  • Index(es):
    • Date
    • Thread