【发布时间】:2012-02-10 10:20:32
【问题描述】:
我正在使用 Cocoa 编写 Mac 应用程序。
如何循环/枚举 NSWindow 中的所有按钮、标签和其他 GUI 控件?我想获取每个控件的标签
谢谢!
【问题讨论】:
标签: macos cocoa user-interface controls
我正在使用 Cocoa 编写 Mac 应用程序。
如何循环/枚举 NSWindow 中的所有按钮、标签和其他 GUI 控件?我想获取每个控件的标签
谢谢!
【问题讨论】:
标签: macos cocoa user-interface controls
我猜你会想要这样的东西:
- (void)addLabelsFromSubviewsOf:(NSView *)view to:(NSMutableArray *)array
{
// get the label from this view, if it has one;
// I'm unsure what test you want here, maybe:
if([view respondsToSelector:@selector(stringValue)])
[array addObject:[view stringValue]];
// or possibly:
// if([view isKindOfClass:[NSTextField class]]) ?
// and traverse all subviews
for(NSView *view in [view subviews])
{
[self addLabelsFromSubviewsOf:view to:array];
}
}
...
NSMutableArray *array = [NSMutableArray array];
[self addLabelsFromSubviewsOf:[window contentView] to:array];
视图可以有子视图,所以它最终是一个树行走。在这段代码中,我只是使用了简单的递归来实现这一点。
【讨论】: