Walking through views

“I’m just walking my beat friend. Mexico is my beat and I’m walking it…” (Johnny Depp / Once Upon a Time in Mexico)

In our case, UIViews are our beat, and we spend a lot of time walking them.

Here’s a simple function to recursively display all subviews within a parent view. It’s very convenient for finding out how many views are hidden within a parent view (such as UIImageViews, UILabels, UIButtons, etc.) — and it’s especially useful when debugging iPad apps with multiple levels of interactivity (such as scrollviews within scrollviews, or accordions within panels within scrollviews)…

- (void) dumpViews:(UIView *)theView withLevel:(int)theLevel {
	// this displays all views in the log
	// example usage:
	// NSLog (@"\r===================================\r===================================\r===================================\r");
	// [self dumpViews:self.view withLevel:0];

	for (int i = 0; i < [[theView subviews] count]; i++) {
		NSString *padding = @"";
		for (int n = 0; n < theLevel; n++) {
			padding = [NSString stringWithFormat:@"%@  ", padding];
		if (theLevel)
			padding = [NSString stringWithFormat:@"%@-", padding];

		NSLog (@"%@%i : %@", padding, theLevel, [[[theView subviews] objectAtIndex:i] description]);
		[self dumpViews:[[theView subviews] objectAtIndex:i] withLevel:theLevel + 1];

About jeffrey

Jeffrey Berthiaume is a multimedia developer and internet architect who has designed and built award-winning websites, kiosks, and content management systems. He bridges the gap between creative and technology with an ability to balance the needs of designers and marketing with the capabilities of existing technology.
This entry was posted in iOS, Tutorials. Bookmark the permalink.

One Response to Walking through views

  1. John Reas says:

    Jeff, we are going to host a DEO at TechWildcatters on 3/26. Hope you will be able to join!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>