对于我的标签栏控制器的第二项,我有一个导航控制器,我想以模态方式呈现。我不断收到错误消息
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序尝试以模态方式呈现活动控制器。'
然后我的应用程序崩溃并转到应用代表。
这是我到目前为止的代码
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
}
答案 0 :(得分:3)
您正在尝试呈现已在UITabBarController中处于活动状态的viewcontroller,这就是应用程序崩溃的原因。 尝试使用此
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
}
答案 1 :(得分:0)
您需要尝试在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
}