首页 > 解决方案 > 将导航控制器推入导航控制器

问题描述

我有一个 ViewController,它由以编程方式构建的屏幕组成。让我们说 FirstVc。在按钮上单击我想要的窗口,我的故事板 中的导航控制器(如图所示)

如图所示,我将 SecondViewController 嵌入到导航控制器中

在 OnButtonClick 函数中使用我当前的代码。它抛出错误无法推送导航控制器内部导航控制器。这是真的。但如果我只从 Main.storyboard 实例化 ViewController,我将无法看到顶部导航栏。

另外,我不想使用 .present ,因为我希望整个窗口发生变化。代表故事板的方括号。

FirstVc <---> [Navigatiob->Tab->SecondVC]

实现虚线表示的链接有哪些可能的方法?

场景代表

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }
//            
                    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
                    window?.windowScene = windowScene

                    let navigation = AppNavigationView()

                    let viewController  = ViewController()

    navigation.pushViewController(viewController, animated: true)
                    window?.rootViewController = navigation
                    window?.makeKeyAndVisible()

}

ViewController 中的功能

    @objc func buttonTapped(){

        let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
        navigationController?.pushViewController(viewController, animated: true)
        self.present(viewController, animated:true, completion:nil)

        print("button tapped")
    }

错误日志

-- 移动到活动状态 --- 2020-04-02 20:49:43.978018+0530 Snug[13340:370457] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”*首先抛出调用堆栈:

标签: iosobjective-cswiftswift3swift4

解决方案


在函数导航中,首先使用 let 常量设置第二个控制器,然后在 pushViewController 中调用它:

@objc func navigate() {
 let secondController = SecondViewController() 
 navigationController?.pushViewController(secondController, animated: true)
}

像这样自然设置场景委托

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let vC = UINavigationController(rootViewController: ViewController())
    window?.rootViewController = vC
}

推荐阅读