开发者

Property Grid for Cocoa

开发者 https://www.devze.com 2022-12-17 17:52 出处:网络
I didn\'t find anything similar to .NET PropertyGrid class in Cocoa, so I started to write my own version.

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:

  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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号