C# Cocoa的属性网格
我在Cocoa中找不到任何类似于.NET PropertyGrid类的东西,所以我开始编写自己的版本。 我使用运行时的信息获取对象的属性:C# Cocoa的属性网格,c#,objective-c,cocoa,reflection,propertygrid,C#,Objective C,Cocoa,Reflection,Propertygrid,我在Cocoa中找不到任何类似于.NET PropertyGrid类的东西,所以我开始编写自己的版本。 我使用运行时的信息获取对象的属性: Class reflectedClass = [reflectedObject class]; uint propertyCount = 0U; objc_property_t *properties = class_copyPropertyList(reflectedClass,
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]];
}
用于将更新与基本KVO同步的reflectedObject
:
[reflectedObject addObserver:self
forKeyPath:propertyName
options:NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionNew
context:NULL];
此解决方案可行,但我有两个问题需要解决:
Cocoa在框架中没有内置这样的视图。如果没有其他人创建了一个并将其作为开源发布,那么您需要从头开始创建一个
手工制作一个与底层模型匹配的UI可能更容易。什么是属性网格,它如何比在IB中为模型创建正确的控件更好?属性网格类似于IB中的inspector,就像在IB中一样,它允许更改任何对象的属性。在.NET中,它通过反射获得任何对象的属性,是UI原型制作的好工具。你可以在以下位置看到一些关于它的信息:我想从运行时动态创建它。是的,我知道了。就像我说的,Cocoa中不存在这样的东西,所以你要么找到一个已经有人制作的开源软件,要么自己制作。好的,谢谢你花时间。我会试着自己写的。