对于标签栏 Controller 的第二项,我有一个想要以模态方式呈现的导航 Controller 。我不断收到错误消息
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .'
然后我的应用程序崩溃并转到应用程序委托(delegate)。
这是我到目前为止的代码
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
}
最佳答案
您正在尝试呈现已在 UITabBarController 中处于事件状态的 View Controller ,这就是应用程序崩溃的原因。 尝试使用这个
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
}
关于ios - 应用程序尝试以模态方式呈现事件 Controller - 导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482320/