忘记从哪里看到的这个了,不过非常有用,记录下来!
// Recursively travel down the view tree, increasing the indentation level for children
- (void)dumpView:(UIView *)aView atIndent:(int)indent into:(NSMutableString *)outstring
{
for (int i = 0; i < indent; i++) [outstring appendString:@"--"];
[outstring appendFormat:@"[%2d] %@ ", indent, [[aView class] description]];
for (UIView *view in [aView subviews])
[self dumpView:view atIndent:indent + 1 into:outstring];
}
// Start the tree recursion at level 0 with the root view
- (NSString *) displayViews: (UIView *) aView
{
NSMutableString *outstring = [[NSMutableString alloc] init];
[self dumpView: aView atIndent:0 into:outstring];
return [outstring autorelease];
}
// Show the tree
- (void)logViewTreeForMainWindow
{
NSLog(@"The view tree: %@", [self displayViews:self.window]);
}
具体用法就是在你想知道你的view的层次的时候,调用一下这个logViewTreeForMainWindow函数就可以了。