• 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: Get selected NSManagedObject from a NSPopUpButton
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get selected NSManagedObject from a NSPopUpButton


  • Subject: Re: Get selected NSManagedObject from a NSPopUpButton
  • From: Gian Marc Cadosch <email@hidden>
  • Date: Sat, 8 Oct 2005 21:49:43 +0200

Thank you for the solution, that works great! Now I'd like to ask you how you implement the reverse case, that is to set the Popup Button from a given NSManagedObject. I currently use selectItemWithTitle on the Popup Button and that works (at least as long as the names are unique). But I'm sure there is a proper solution...

Marc


That seems like more work than is necessary since you can simply call:

[[controller selectedObjects] objectAtIndex:0];

Checking the count prior to that if it is possible that nothing is selected.

Why retrieved all of the objects in the controller when you can just
retrieve the selected objects which in this case would always be a
single object (or nil)?

Using selectedObjects: would eliminate your step two completely and
make your step three cleaner.  Although I do like the idea of adding a
category to retrieve a single object, makes the code a little cleaner.

On 10/8/05, Niko Matsakis <email@hidden> wrote:

More specifically, what I do is this:

0. Bind the values binding from button to controller
1. Bind the "selectedIndex" from the button to the controller's
selectedIndex
2. To get the current selected item, do either:

     [controller selection]

     or

     [[controller arrangedObjects] objectAtIndex:[controller
selectionIndex]];

For part 2, the two choices are basically equivalent, but the former
returns a proxy and the latter returns the actual NSManagedObject, so
I prefer the latter. In fact, I have created a handy category for it:


@implementation NSArrayController (XXX)

- (id) selectedObject
{
     unsigned int selidx = [self selectionIndex];
     if (selidx != NSNotFound)
         return [[self arrangedObjects] objectAtIndex:selidx];
     return nil;
}

@end

There may be a better way to do this, but this is the best one I have
found.  It also does not involve having any IBOutlets pointing to the
actual NSPopUpButton, which seems clean to my sensibilities.



Niko



On Oct 8, 2005, at 4:25 PM, Marcus S. Zarra wrote:


Make sure you have a reference to the NSArrayController then you can
call selectedObjects on the array controller which will give you the
selected objects as an array.

On 10/8/05, Gian Marc Cadosch <email@hidden> wrote:


I've found lots of good information for programming core data
applications. But now I have a problem for which I  haven't found a
solution yet:

I'd like to get the managed object which is selected in a popupmenu.
The popup menu is bound to an NSArrayController which receives the
data from core data.


Thanks for helping.

Marc
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden






_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Get selected NSManagedObject from a NSPopUpButton
      • From: "Marcus S. Zarra" <email@hidden>
References: 
 >Get selected NSManagedObject from a NSPopUpButton (From: Gian Marc Cadosch <email@hidden>)
 >Re: Get selected NSManagedObject from a NSPopUpButton (From: "Marcus S. Zarra" <email@hidden>)
 >Re: Get selected NSManagedObject from a NSPopUpButton (From: Niko Matsakis <email@hidden>)
 >Re: Get selected NSManagedObject from a NSPopUpButton (From: "Marcus S. Zarra" <email@hidden>)

  • Prev by Date: Re: Core Data notifications
  • Next by Date: Re: Get selected NSManagedObject from a NSPopUpButton
  • Previous by thread: Re: Get selected NSManagedObject from a NSPopUpButton
  • Next by thread: Re: Get selected NSManagedObject from a NSPopUpButton
  • Index(es):
    • Date
    • Thread