0

我在 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];

此解决方案有效,但我有两个问题需要解决:

  1. 我需要以某种方式模拟 .NET 属性,因此我可以为属性选择正确的编辑器。文本框并不适用于所有情况。
  2. 每行都有不同的单元格编辑器,因此对于布尔复选框、字符串文本框等。

我仍然是 Cocoa 的初学者,如果我要求一些非常基本的东西,我很抱歉。

更新:我需要这样的东西(图片来自 Xcode->Get Info->Build):

PropertyGridCocoa http://www.adorior.cz/Images/PropertyGridCocoa.png

4

1 回答 1

1

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.

于 2010-01-17T02:18:32.550 回答