获取NSWindow的所有视图和子视图

use*_*064 12 cocoa objective-c nsview nswindow

有没有办法可以获得NSWindow这些子视图的所有视图,子视图和子视图(你得到了想法......)?

谢谢.

NSG*_*God 10

这是NSView上的一个类别:

@interface NSView (MDRecursiveSubviews)
- (NSArray *)allSubviews;
@end

@implementation NSView (MDRecursiveSubviews)

- (NSArray *)allSubviews {
    NSMutableArray *allSubviews = [NSMutableArray arrayWithObject:self];
    NSArray *subviews = [self subviews];
    for (NSView *view in subviews) {
        [allSubviews addObjectsFromArray:[view allSubviews]];
    }
    return [[allSubviews copy] autorelease];
}

@end
Run Code Online (Sandbox Code Playgroud)

使用我使用视图层次结构创建的快速nib文件,它打印出:

[RecursiveSubviewsAppDelegate awakeFromNib] allSubviews == (
    "<NSView: 0x10390dfd0>",
    "<NSView: 0x103c07ae0>",
    "<NSView: 0x100129cc0>",
    "<NSButton: 0x100115ce0>",
    "<NSButton: 0x100116900>",
    "<NSButton: 0x1001165c0>",
    "<NSButton: 0x100116130>",
    "<NSButton: 0x100114ad0>",
    "<NSButton: 0x100115910>",
    "<NSButton: 0x100115090>",
    "<NSScrollView: 0x103b07a30>",
    "<NSClipView: 0x103b07d40>",
    "<NSTextView: 0x103b083c0>\n
Frame = {{0.00, 0.00}, {159.00, 58.00}},
Bounds = {{0.00, 0.00}, {159.00, 58.00}}\n
Horizontally resizable: NO, Vertically resizable: YES\n
MinSize = {159.00, 58.00}, MaxSize = {463.00, 10000000.00}\n",
    "<NSScroller: 0x1001145b0>",
    "<NSScroller: 0x100114840>",
    "<NSScrollView: 0x10390ea00>",
    "<NSClipView: 0x10390ef10>",
    "<NSTableView: 0x10390f570>",
    "<NSScroller: 0x103b06f10>",
    "<NSScroller: 0x103b07460>",
    "<NSClipView: 0x1039105d0>",
    "<NSTableHeaderView: 0x103910300>",
    "<_NSCornerView: 0x103911c20>"
Run Code Online (Sandbox Code Playgroud)

我应该补充的一个值得关注的问题是,除了作为调试工具之外,我不清楚它是如何有用的.但即便如此,也许有更简单的做事方式.


小智 8

可以发送一条私人消息NSView来打印控件层次结构.

[NSView _subtreeDescription]为您提供NSView其子女及其子女的整体等级.