Sean McMains
Sean McMains

Reputation: 59183

Can't Get Count of viewControllers

I'm trying to iterate through the controllers that my UITabBarController is managing, and for the tabs that have UINavigationControllers contained therein, see how many viewControllers are currently on the navigation stack. Unfortunately, while the controllers claim to be UINavigationControllers, and their viewControllers property appears to be an array, it doesn't respond to "count" as I'd expect:

    for ( UIViewController *controller in tabBarController.viewControllers ) {
    if ( [controller isKindOfClass:[UINavigationController class]] ) {
        UINavigationController *navigationController = (UINavigationController*)controller;
        NSLog(@"Analyzing controller: %@", controller.title);
        NSLog(@"Views in hierarchy: %@", [navigationController.viewControllers count]);
    } else {
                ....
            }
    }

I get an EXC_BAD_ACCESS error on the "Views in hierarchy:" line when it tries to send the count message to navigationController.viewControllers.

I'm baffled here, and would love any help anyone can offer. Thanks in advance!

Upvotes: 1

Views: 3368

Answers (1)

Justin Spahr-Summers
Justin Spahr-Summers

Reputation: 16973

You're using the %@ format specifier to print out the result of -count, which returns an NSUInteger. %@ is suitable only for printing out objects, and so it'll be expecting an object even as you provide it only an integer. You'll have to do something like this instead:

NSLog(@"Views in hierarchy: %lu",
    (unsigned long)[navigationController.viewControllers count]);

Upvotes: 4

Related Questions