0

I’m trying to write a text file with name of all the views contains in the Screen. For example I have a UIView in which I have 2 UIButtons so the file should be as bellow..

UIWindow
    UIView
        UIButton
            UIButtonLabel
        UIButton
            UIButtonLabel
        _UILayoutGuide
        _UILayoutGuide

This is how I'm trying to do but the above hierarchy I'm unable to achieve.

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

this is not something I'm using in any project but doing it just for fun. any suggestion would be great.

Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
  • for (UIView *view in [self.view subviews]) {...} this way u would get all subviews in a single view. So as u move from one view to another search about these subview and do perform ur operation – nikhil84 Sep 05 '14 at 11:57
  • @walle84 problem is not to get subview, I know we already have methods for that. but the problem is to dump those subview names in to a text file, according to hierarchy level (mention in question). – Suryakant Sharma Sep 05 '14 at 12:09

1 Answers1

3

You probably won't be able to submit apps to the app store using the following code (which it sounds like you don't want anyways) since it's undocumented private API, but you can use this in your debugging:

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

you can also use recursiveDescription in the LLDB console for debugging when you're on a breakpoint:

po [someView recursiveDescription]

armcknight
  • 149
  • 6