views:

213

answers:

1

I didn't find anything similar to .NET PropertyGrid class in Cocoa, so I started to write my own version. I use information from runtime to get properties of object:

Class reflectedClass = [reflectedObject class];
uint propertyCount = 0U;
objc_property_t *properties = class_copyPropertyList(reflectedClass, 
                                                     &propertyCount);

And this for getting/setting values in NSTableView:

- (NSString *)propertyNameAtIndex:(int)index
{
    return (NSString *)[cachedPropertyNames objectAtIndex:index];
}

- (id)propertyValueAtIndex:(int)index
{
    return [reflectedObject valueForKey:[self propertyNameAtIndex:index]];
}

- (void)setPropertyValue:(id)value atIndex:(int)index
{
    [reflectedObject setValue:value forKey:[self propertyNameAtIndex:index]];
}

For syncing updates with reflectedObject is used basic KVO:

[reflectedObject addObserver:self
                  forKeyPath:propertyName
                     options:NSKeyValueObservingOptionOld | 
                             NSKeyValueObservingOptionNew
                     context:NULL];

This solution works, but I have two problems that I need to fix:

  1. I need to simulate somehow .NET attributes, so I can choose right editor for property. Text boxes is not good for all situations.
  2. Different cell editor for each row, so for booleans checkboxes, for strings textboxes, etc.

I am still beginner in Cocoa so sorry if I am asking for something really basic.

UPDATE: I need something like this (picture from Xcode->Get Info->Build):

PropertyGridCocoa

+1  A: 

Cocoa has no such view built in to the framework. If no-one else has created one and released it as open source, you will need to create one from the ground up.

It's probably easier to hand-craft a UI that matches the underlying model.

Peter Hosey
I want to create it on the fly from runtime.
Filip Kunc
Yeah, I got that. Like I said, no such thing exists in Cocoa, so you'll have to either find an open-source one that someone already made or make one yourself.
Peter Hosey
Ok, thank you for your time. I will try to write it on my own.
Filip Kunc