2012-01-14 21 views

回答

3

我想你想要的東西,如:

- (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]; 

視圖可以有子視圖,所以它最終被一棵樹的步行路程。在這段代碼中,我只是使用簡單的遞歸來實現這一點。

+0

嗨湯米,謝謝,你的解決方案看起來不錯。當我嘗試使用NSWindow的contentView時,其子視圖實際上是空的,我無法訪問它上面的NSButton。你會幫忙嗎?謝謝 – shader 2012-01-14 17:33:11

+0

這真的很奇怪。如果你NSLog的contentView你有什麼合適的? – Tommy 2012-01-14 17:46:45

+0

輸出是 shader 2012-01-14 18:10:30

相關問題