[objective-c] "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

This question is related to objective-c ios ios5

The answer is


Just remove

[tabBarController presentModalViewController:viewController animated:YES];

and keep

[self dismissModalViewControllerAnimated:YES];

The same problem error happened to me when I tried to present a child view controller instead of its UINavigationViewController parent


I have the same problem. I try to present view controller just after dismissing.

[self dismissModalViewControllerAnimated:YES];

When I try to do it without animation it works perfectly so the problem is that controller is still alive. I think that the best solution is to use dismissViewControllerAnimated:completion: for iOS5


I had same problem.I solve it. You can try This code:

[tabBarController setSelectedIndex:1];
[self dismissModalViewControllerAnimated:YES];

In my case, I was presenting the rootViewController of an UINavigationController when I was supposed to present the UINavigationController itself.


Instead of using:

self.present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)

you can use:

self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)

In my case i was trying to present the viewController (i have the reference of the viewController in the TabBarViewController) from different view controllers and it was crashing with the above message. In that case to avoid presenting you can use

viewController.isBeingPresented

!viewController.isBeingPresented {
          // Present your ViewController only if its not present to the user currently.
}

Might help someone.


Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to ios5

How to select Multiple images from UIImagePickerController How to get root view controller? Adding Image to xCode by dragging it from File Code signing is required for product type 'Application' in SDK 'iOS5.1' How to Implement Custom Table View Section Headers and Footers with Storyboard Objective-C ARC: strong vs retain and weak vs assign Custom Cell Row Height setting in storyboard is not responding How do I deserialize a JSON string into an NSDictionary? (For iOS 5+) What is the iOS 5.0 user agent string? "Application tried to present modally an active controller"?