“Application tried to present modally an active controller” Error in iOS5

10,719

Solution 1

You are getting this error because you are attempting to display the 'map' view controller twice. The first time is as the root view controller of 'navigationController' and the second time is via [split presentModalViewController:map animated:YES].

iOS 5 is being a bit more picky than iOS 4 when you try to do strange things with view controllers. Trying to show the same controller twice is a design problem - you need to work out what you are really trying to do and fix it.

(Also, calling a map view controller 'MapView' rather than 'MapViewController' is really confusing)

Solution 2

This error will also occur if you don't follow these guidelines: Creating Custom Content View Controllers

Basically, you need to call:

[yourVC removeFromParentViewController];

if you've

[parentVC addChildViewController:yourVC];

This error may often be paired with something about "UIViewControllerHierarchyInconsistency"

Share:
10,719
GuybrushThreepwood
Author by

GuybrushThreepwood

iPhone and Android developer with 4.0 years experience.

Updated on June 04, 2022

Comments

  • GuybrushThreepwood
    GuybrushThreepwood about 2 years

    I have an error which is causing my app to crash under iOS5 only on the iPad.

    The below code is called when the user taps on an item in a uibarbutton item :

    - (void)optionSelected:(NSString *)option {
    
    [self.optionPickerPopover dismissPopoverAnimated:YES];
    
    if ([option compare:@"Map View"] == NSOrderedSame) {
        NSLog(@"Map View"); 
        MapView * map = [[MapView alloc] initWithNibName:@"MapView" bundle:nil]; 
    
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:map];
    
        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                        style:UIBarButtonItemStyleDone target:self action:@selector(removeCurrent)];
        map.navigationItem.rightBarButtonItem = rightButton;
    
        [self presentModalViewController:navigationController animated:YES];
    
        [navigationController release];
        [map release];     
        [rightButton release];
        [split presentModalViewController:map animated:YES];
    }
    

    Can anyone suggest why this occurring in iOS5 ?