Search code examples
objective-ciosios5

"Application tried to present modally an active controller"?


I just came across a crash showing a NSInvalidArgumentException with this message on an app which wasn't doing this before.

Application tried to present modally an active controller UITabBarController: 0x83d7f00.

I have a UITabBarController which I create in the AppDelegate and give it the array of UIViewControllers.

One of them I want to present modally when tapped on it. I did that by implementing the delegate method

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

If that view controller is of the class of the one I want to present modally, I return NO and do

[tabBarController presentModalViewController:viewController animated:YES];

And now I'm getting that error, which seems to mean that you can't present modally a view controller that is active somewhere else (in the tabbar...) I should say I'm on XCode 4.2 Developer Preview 7, so this is iOS 5 (I know about the NDA, but I think I'm not giving any forbidden details). I currently don't have an XCode installation to test if this crashes compiling against the iOS4 SDK, but I'm almost entirely sure it doesn't.

I only wanted to ask if anyone has experienced this issue or has any suggestion


Solution

  • Assume you have three view controllers instantiated like so:

    UIViewController* vc1 = [[UIViewController alloc] init];
    UIViewController* vc2 = [[UIViewController alloc] init];
    UIViewController* vc3 = [[UIViewController alloc] init];
    

    You have added them to a tab bar like this:

    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, nil]];
    

    Now you are trying to do something like this:

    [tabBarController presentModalViewController:vc3];
    

    This will give you an error because that Tab Bar Controller has a death grip on the view controller that you gave it. You can either not add it to the array of view controllers on the tab bar, or you can not present it modally.

    Apple expects you to treat their UI elements in a certain way. This is probably buried in the Human Interface Guidelines somewhere as a "don't do this because we aren't expecting you to ever want to do this".