ios - 应用程序尝试以模态方式呈现事件 Controller - 导航 Controller

标签 ios swift xcode uinavigationcontroller

对于标签栏 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
}

This is how my navigation controller is connected to my Tab Bar Controller

最佳答案

您正在尝试呈现已在 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/

相关文章:

iphone - 如何在iOS中自定义搜索键盘?

ios - 使用 UITableViewCell 的自动布局约束以显示两个 View

ios - Xcode 5,xib本地化导致手机黑屏

ios - CNContactStoreDidChangeNotification 被多次触发

iOS 约束方程值

ios - 在 imageview 中加载来自互联网的图像的函数

ios - 点击 ParentViewController 关闭 ChildViewController

ios - 使用 UIImageView+AFNetworking,图像直到滚动才出现

Android 的 addJavascriptInterface 在 iOS 中的等价物

用于启动 URL 的 Xcode 按钮