Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding "Not Selected" Option To Bound NSPopupButton
    primarykey
    data
    text
    <p>I have an NSPopupButton that is bound to a core data backed NSArrayController. The selection of the NSPopupButton is also bound to a core data backed item. Bindings pretty much as follows:</p> <pre> NSArray 'A' of core data entities --> Array Controller Array Controller --> NSPopupButton.contentValues.arrangedObjects.name NSArray 'B' of core data entities --> Array Controller Array Controller --> NSPopupButton.selectedObject.methodOnObject.name </pre> <p>The selectedObject has a method on it that looks up an ID that finds an object and returns that object as follows:</p> <pre><code>-(MyWidget *)selectedWidget { return [MyCunningLookupMethod widgetForID:[[self widgetID] intValue]]; } </code></pre> <p>And setting the object the other way is as simple as:</p> <pre><code>-(void)setSWidget:(MyWidget *)anObj { [self setWidgetID:[anObj uid]]; } </code></pre> <p>Most of the time the object matches an object in the available contentValues list. However, there are times when the ID of the selected object is 0 - in which case I want to display an option in the available list called "Not Selected". I could easily accomplish sending back a different object (or nothing) if the object ID is 0.</p> <p>I can handle this with a "No selection placeholder", but as soon as the user selects one of the other items, the placeholder for not selected is removed from the list.</p> <p>I want to be able to provide the user with the ability to choose an item, or choose not to have an item selected (i.e. set it back to "Not Selected"). Short of creating the entire NSPopupMenu programmatically by walking the array I get from core data each time the selection changes, is there a way to insert a menu item into the list that represents a not selected state that will always be available to the user?</p> <p>I have considered adding an entity object into the core data store that has all 0-based values with the exception of the name which would say "Not Selected". This, however, just doesn't feel like the right way to do things and actually presents some other issues about having empty objects in the store that don't actually have any data relevance.</p> <p>As always, any and all help would be much appreciated.</p> <p><strong>SOLUTION</strong></p> <p>Well I didn't exactly follow what <code>Hobbes the Tige</code> posted, but it pretty much put me where I needed to be. Instead of binding in IB I ended up creating a method that allows me to send in my array of objects on a selection change to a parent array or user activity initiating a change. The method then simply updates the NSPopupButton with the appropriate core data entity information and still allows me to us IB to bind the selectedTag of the matching object.</p> <p>Here's the method</p> <pre><code>+(void)createContentsForPopup:(NSPopUpButton *)aPopupButton withArray:(NSArray *)anObjectArray addNotSelected:(BOOL)aFlag { [aPopupButton removeAllItems]; if (aFlag) { if (!anObjectArray || [anObjectArray count] == 0) { [aPopupButton addItemWithTitle:@"--- Not available ---"]; [[aPopupButton itemAtIndex:0] setTag:0]; return; } [aPopupButton addItemWithTitle:@"--- Not selected ---"]; [[aPopupButton itemAtIndex:0] setTag:0]; } NSSortDescriptor * sd = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; [anObjectArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sd]]; for (id anObject in anObjectArray) { [aPopupButton addItemWithTitle:[anObject name]]; int thisItem = [aPopupButton indexOfItemWithTitle:[anObject name]]; [[aPopupButton itemAtIndex:thisItem] setTag:[[anObject uid] intValue]]; } } </code></pre> <p>Obviously this depends upon any array of objects being passed in conforming to having a <code>name</code> for the descriptive field and a <code>uid</code> for the unique identifier.</p> <p>Job done. :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload