可可的属性网格
我在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->获取信息->构建):
PropertyGridCocoa http://www.adorior.cz/Images/PropertyGridCocoa.png
I didn't find anything similar to .NET PropertyGrid class in Cocoa, so I started to write my own version.
I use information from 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cocoa 的框架中没有内置这样的视图。如果没有其他人创建一个并将其作为开源发布,那么您将需要从头开始创建一个。
手工制作与底层模型相匹配的 UI 可能更容易。
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.