Month: February 2014

Debugging Complex iOS View Issues

Debugging Complex iOS View Issues

At certain times throughout a software developer’s career, some things just don’t make sense. All of a sudden something stops working the way it used to, and you have no idea why. This was the case when a button my UITableViewCell was no longer responding to any touch events. I checked the IBOutlet and IBAction from my UIButton 4 or 5 times to see if they were set up correctly. I even re-did the bindings just to make sure they were OK. My breakpoint was not being hit when I tapped the button. I was perplexed and stumped! As George Costanza would say, “These pretzels… are making me thirsty!”

I remembered a tool demoed at the Denver iOS Meetup back in January to debug complex iOS view issues. The tool is called Spark Inspector. Spark Inspector allows you to see the class hierarchy of any view you are currently in when running the simulator. It also allows you to play with the properties of the class to experiment having different options turned on/off.

The following is a screenshot of the view I was having trouble with. As you can see Spark Inspector has an option to view your view hierarchy is 3D, which is awesome.

SparkViewDebugging

As you can see, there is an unwanted view sitting on front my button I want to be able to tap. The button is the star in the upper right and bottom section of the view. The view sitting in front of everything is a class called UITableViewCellContentView. From what I’ve read this class is new in iOS7 to help certain interactions such as found here.

After messing around with the properties of the class in Spark Inspector, I found that setting the Alpha to 0 will allow me to interact with my button again! This was a great discovery, but actually solving the problem in my code was not trivial. Within the class that loads my UITableViewCell from a xib, I got into the sub views until I find the culprit and then set the Alpha to 0.

    NSArray *subviews= [self subviews];

    NSLog(@"Subviews count: %d",subviews.count);

    for (UIView *view in subviews)
    {
        NSLog(@"CLASS: %@",[view class]);
        NSArray *otherViews = [view subviews];
        NSLog(@"Sub-Subviews count: %d",otherViews.count);

        if (otherViews.count > 1) {
            if ([otherViews[1] respondsToSelector:@selector(setAlpha:)])
            {
                [otherViews[1] setAlpha:0];
            }
        }
    }

This did the trick. There may be a more elegant to solve this problem, but I thought I’d share this solution with others if you come across a problem such as this one. I’d like to hear any other suggestions on debugging iOS view issues if you’ve had a similar experience.