我在 Cocoa 中没有找到类似 .NET PropertyGrid 类的东西,所以我开始编写自己的版本。我使用来自运行时的信息来获取对象的属性:
Class reflectedClass = [reflectedObject class];
uint propertyCount = 0U;
objc_property_t *properties = class_copyPropertyList(reflectedClass,
&propertyCount);
这用于在 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]];
}
reflectedObject
使用基本 KVO同步更新:
[reflectedObject addObserver:self
forKeyPath:propertyName
options:NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionNew
context:NULL];
此解决方案有效,但我有两个问题需要解决:
- 我需要以某种方式模拟 .NET 属性,因此我可以为属性选择正确的编辑器。文本框并不适用于所有情况。
- 每行都有不同的单元格编辑器,因此对于布尔复选框、字符串文本框等。
我仍然是 Cocoa 的初学者,如果我要求一些非常基本的东西,我很抱歉。
更新:我需要这样的东西(图片来自 Xcode->Get Info->Build):
PropertyGridCocoa http://www.adorior.cz/Images/PropertyGridCocoa.png