0

Id like to use a NSPopupbutton to show a list. each element in the list combine two attributes from an NSManagedObject.

"firstname, userid"

I'd rather not add a transient attribute, since that dirties the business logic.

Is there a simple method of merging two values for the Content Values in the NSPopupbutton

It looks like NSLabel has a DisplayPattern. Nevertheless, that also appears to permit only binding to one attribute, although it might accommodate the string formatting.

OSX not iOS.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Gabe Rainbow
  • 3,658
  • 4
  • 32
  • 42
  • why not to use a property and bind that to NSpopup. And that property will use nsstring appendWithFormat from your managedobject? You can also use valueTransformer class – Anoop Vaidya Jun 10 '13 at 17:55
  • i was hoping to avoid introducing business logic or state -- given thats its UI appearance. `nsvaluetransformer` looks possible. is that an object that can be placed into a nib and bound via IB. – Gabe Rainbow Jun 10 '13 at 19:24
  • is see that nspopup (and nslabel) has a value transformer option. are you familiar with the best sample code. – Gabe Rainbow Jun 10 '13 at 19:34
  • it looks like the 'Temperature Converter' sample is the best example. cheers – Gabe Rainbow Jun 10 '13 at 19:50

1 Answers1

0

Once common way to do this while keeping the view code separate from the model code is to use Objective-C categories. If your managed object subclass were named MyManagedObject, you would do something like this:

@interface MyManagedObject (ViewSupport)

@property (nonatomic, readonly) NSString* stringForPopUpButton;

@end


@implementation MyManagedObject (ViewSupport)

+ (NSSet *)keyPathsForValuesAffectingStringForPopUpButton
{
    return [NSSet setWithObjects:@"firstname", @"userid", nil];
}

- (NSString *)stringForPopUpButton
{
    return [NSString stringWithFormat: @"%@, %@", self.firstname, self.userid];
}

@end

This code can appear in a separate file, or whatever. This pattern works well for simple things like combining two properties. For more complex transformations, the logic will usually live in an NSViewController subclass. The model is typically pushed into these objects using the -representedObject property and then the accompanying views can be constructed in Xcode/InterfaceBuilder, etc. This pattern keeps the data munging logic coupled with the view/nib/xib that uses it.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • I've done something similar in the past. Having a property in a Category. Categories are awesome (tm). But skipped over the keyPathsForValuesAffectingXXX procedure. Is that something to do with KVO. cheers – Gabe Rainbow Jun 16 '13 at 03:32
  • Yes, it's for KVO. See here: https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/KeyValueObserving/Articles/KVODependentKeys.html – ipmcc Jun 16 '13 at 11:01
  • Hayzeus. Thats what im seeking. So is that how transient properties are often updated -- should they rely on other props. For example the 'fullname' in that document could be a transient property on a managedobject.... – Gabe Rainbow Jun 17 '13 at 22:05
  • further im guessing that is convenience for adding an observer in awakeFromInsert and doing the whole observeValueForKeyPath yaddayadda – Gabe Rainbow Jun 17 '13 at 22:09
  • morese, im hoping one can do something along the lines of return [NSSet setWithObjects:@"employees.firstname", @"employees.userid", nil]; although that clearly would be a performance drain – Gabe Rainbow Jun 17 '13 at 22:13