xcode 应用程序试图以模态方式呈现活动控制器 - 导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47482320/
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 - Navigation Controller
提问by vApp
For my second item of my tab bar controller, I have a navigation controller that I want to present modally. I keep getting an error that says
对于我的标签栏控制器的第二个项目,我有一个导航控制器,我想以模态方式呈现。我不断收到一个错误,说
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .'
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图以模态方式呈现活动控制器。”
Then my app crashes and goes to the app delegate.
然后我的应用程序崩溃并转到应用程序委托。
This is the code I have so far
这是我到目前为止的代码
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let restoreID = viewController.restorationIdentifier {
if restoreID == "NavigationCamera" {
if let nav = tabBarController.viewControllers![tabBarController.selectedIndex] as? UINavigationController {
print("Nav is allowed")
//let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "CameraView")
tabBarController.present(nav, animated: true, completion: {
print("complete")
})
return false
}
}
}
return true
}
回答by Khairul Islam
You are trying to present viewcontroller which is already active in UITabBarController, that's why app crashed. Try using this
您正在尝试呈现已经在 UITabBarController 中处于活动状态的视图控制器,这就是应用程序崩溃的原因。尝试使用这个
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let restoreID = viewController.restorationIdentifier {
if restoreID == "NavigationCamera" {
print("Nav is allowed")
let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "CameraView") as! UIViewController
tabBarController.present(UINavigationController.init(rootViewController: newVC), animated: true, completion: {
print("complete")
})
return false
}
}
return true
}
回答by Justin Snider
You need to try and change the selectedViewController property within the tabBarController rather then using the present method. Try this instead
您需要尝试更改 tabBarController 中的 selectedViewController 属性,而不是使用本方法。试试这个
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let restoreID = viewController.restorationIdentifier {
if let restoreID == "NavigationCamera" {
print("Nav is allowed")
tabBarController.selectedViewController = tabBarController.viewControllers![tabBarController.selectedIndex] as? UINavigationController
return false
}
}
return true
}