Property Grid for Cocoa
I didn't find anything similar to .NET PropertyGrid class in Cocoa, so I started to write my own version. I use information fro开发者_高级运维m 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):
PropertyGridCocoa http://www.adorior.cz/Images/PropertyGridCocoa.png
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.
精彩评论