0

我正在尝试编写一个文本文件,其中包含屏幕中包含的所有视图的名称。例如,我有一个UIView,其中有 2 个UIButtons,所以文件应该如下所示。

UIWindow
    UIView
        UIButton
            UIButtonLabel
        UIButton
            UIButtonLabel
        _UILayoutGuide
        _UILayoutGuide

这就是我想要做的,但我无法实现上述层次结构。

-(void) writeViewHierarchyForView:(UIView *)aView inString:( NSMutableString *)str
{
    //NSMutableString *str = [[NSMutableString alloc] init];
    NSLog(@"title Text = %@", aView.titleText);
    if(nil == aView.children){
        [str appendString:@"\n"];
        return;
    }
    else{
        for(UIView *view in aView.subview){
            [str appendString:@"\n\t"];
            [str appendString:view.name];
            NSLog(@"%@",view.name);
            [str appendString:@"\t"];
            [self writeViewHierarchyForView:view inString:str];
        }
    }
}

这不是我在任何项目中使用的东西,只是为了好玩。任何建议都会很棒。

4

1 回答 1

3

您可能无法使用以下代码将应用程序提交到应用程序商店(听起来您无论如何都不想要),因为它是未记录的私有 API,但您可以在调试中使用它:

NSLog(@"%@", [self.view valueForKey:@"recursiveDescription"]);

您还可以recursiveDescription在断点处使用 LLDB 控制台进行调试:

po [someView recursiveDescription]

于 2014-09-06T02:29:26.130 回答