“应用程序试图提供模态主动控制器”?

我只是偶然发现一个应用程序的 NSInvalidArgumentException出现了崩溃,这个应用程序之前没有这个功能。

应用试图提出一个模态主动控制器 UITabBarController: 0x83d7f00.

我有一个在 AppDelegate中创建的 UITabBarController,并给它 UIViewControllers的数组。

其中一个我想在点击的时候用模态方式呈现,我通过实现委托方法实现了这一点

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

如果那个视图控制器是我想模态地呈现的类,我返回 NO 并执行

[tabBarController presentModalViewController:viewController animated:YES];

现在我得到了这个错误,这似乎意味着您不能模态地显示在其他地方(在选项卡中...)处于活动状态的视图控制器 我应该说我使用的是 XCode 4.2 Developer Preview 7,所以这是 iOS 5(我知道保密协议,但我想我没有给出任何禁止的细节)。目前我还没有安装 XCode 来测试这个程序是否会在 iOS4SDK 上编译崩溃,但是我几乎可以肯定它不会崩溃。

我只是想问问是否有人经历过这个问题或有任何建议

130080 次浏览

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".

Just remove

[tabBarController presentModalViewController:viewController animated:YES];

and keep

[self dismissModalViewControllerAnimated:YES];

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 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.

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

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 presenting the rootViewController of an UINavigationController when I was supposed to present the UINavigationController itself.

For React Native Developer - Problem might not be in AppDelegate Or main.m if app has been successfully build and is running and will crash after splash or perhaps the error screen

Issue might be due to use of fonts/resources that is not available with xcode and not properly configured.. You can find out the error by commenting certain portion starting from App.js and drilling inside the navigation/screens and commenting the components till you find the component that is generating the error....

In my case the resource of fontFamily was making an issue which was used right after splash in walkthrough screen

<Text style=\{\{fontFamily: Fonts.roboto}}>ABC</Text>

Here font roboto wasnot configured properly. Wasted entire days just debugging the error hope its helps you

This is my way which supporting multiple Windows(from a single APP) on the iPad and nested modal present.

import UIKit


///✅Use this public method
public func SheetViewController(ViewController:UIViewController) {
    

for i in returnAvailableViewControllers().shuffled() {
if i.presentedViewController == nil && !ViewController.isViewLoaded {i.present(ViewController, animated: true, completion:  {})}
}
}


///Returns all possible ViewControllers
private func returnAvailableViewControllers() -> [UIViewController] {
let 场景 = UIApplication.shared.connectedScenes
    

var 存储VC : [UIViewController] = []


for i in 场景 {
        

if i.activationState == .foregroundActive {
//⭐️Set up “foregroundActive” to give the user more control
var 视图控制器 = (i.delegate as? UIWindowSceneDelegate)?.window??.rootViewController
            

if 视图控制器 != nil {
存储VC.append(视图控制器!)
}
            

            

            

var 结束没 = true
while 结束没 {
//🌟Enumerate all child ViewController
视图控制器 = 视图控制器?.presentedViewController
if 视图控制器 != nil {
存储VC.append(视图控制器!)
} else {
结束没.toggle()
}
}
}
        

        

}
return 存储VC
}