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:
- I need to simulate somehow .NET attributes, so I can choose right editor for property. Text boxes is not good for all situations.
- 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):