Re: Get selected NSManagedObject from a NSPopUpButton
Re: Get selected NSManagedObject from a NSPopUpButton
- Subject: Re: Get selected NSManagedObject from a NSPopUpButton
- From: "Marcus S. Zarra" <email@hidden>
- Date: Sat, 8 Oct 2005 10:47:06 -0600
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