ios “应用程序试图以模态方式呈现主动控制器”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7429014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Application tried to present modally an active controller"?
提问by Javier Soto
I just came across a crash showing a NSInvalidArgumentException
with this message on an app which wasn't doing this before.
我刚刚在一个NSInvalidArgumentException
以前没有这样做的应用程序上遇到了一个显示此消息的崩溃。
Application tried to present modally an active controller UITabBarController: 0x83d7f00.
应用程序试图以模态方式呈现一个活动控制器 UITabBarController: 0x83d7f00。
I have a UITabBarController
which I create in the AppDelegate
and give it the array of UIViewControllers
.
我有一个UITabBarController
我在 中创建的AppDelegate
并将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
如果该视图控制器属于我想以模态呈现的那个类,我返回 NO 并执行
[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.
现在我收到了那个错误,这似乎意味着你不能以模态方式呈现一个在其他地方(在标签栏中...)处于活动状态的视图控制器,我应该说我在 XCode 4.2 Developer Preview 7 上,所以这是 iOS 5(我知道 NDA,但我想我没有提供任何禁止的细节)。我目前没有安装 XCode 来测试是否会在针对 iOS4 SDK 编译时崩溃,但我几乎完全确定它不会。
I only wanted to ask if anyone has experienced this issue or has any suggestion
我只想问有没有人遇到过这个问题或者有什么建议
回答by lswank
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.
这会给你一个错误,因为 Tab Bar Controller 对你给它的视图控制器有一个死亡控制。您可以不将它添加到选项卡栏上的视图控制器数组,或者您不能以模态方式呈现它。
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".
Apple 希望你以某种方式对待他们的 UI 元素。这可能被埋在人机界面指南的某个地方,因为“不要这样做,因为我们不希望您想要这样做”。
回答by Danil
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
当我尝试在没有动画的情况下执行此操作时,它可以完美运行,因此问题在于控制器还活着。我认为最好的解决方案是dismissViewControllerAnimated:completion:
用于 iOS5
回答by Karthick Ramesh
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(我在 TabBarViewController 中有 viewController 的引用),但它因上述消息而崩溃。在这种情况下,为了避免呈现,您可以使用
viewController.isBeingPresented
viewController.isBeingPresented
!viewController.isBeingPresented {
// Present your ViewController only if its not present to the user currently.
}
Might help someone.
可能会帮助某人。
回答by Erhan Demirci
I had same problem.I solve it. You can try This code:
我有同样的问题,我解决了。你可以试试这个代码:
[tabBarController setSelectedIndex:1];
[self dismissModalViewControllerAnimated:YES];
回答by Nik
The same problem error happened to me when I tried to present
a child view controller instead of its UINavigationViewController
parent
当我尝试present
使用子视图控制器而不是UINavigationViewController
其父视图时,同样的问题发生在我身上
回答by Wimukthi Rajapaksha
Instead of using self.present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)
, you can use self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
.
self.present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)
您可以使用 ,而不是使用self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
。
回答by user170317
Just remove
只需删除
[tabBarController presentModalViewController:viewController animated:YES];
and keep
并保持
[self dismissModalViewControllerAnimated:YES];