将导航控制器推入导航控制器

时间:2020-04-02 14:47:41

标签: ios objective-c swift swift3 swift4

我有一个ViewController,它由以编程方式构建的屏幕组成。 在“按钮”上,单击“我要在窗口上”,单击“情节提要”中的“导航控制器” (As Seen in the Picture)

如图所示,我将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”而终止应用程序,原因:“不支持按导航控制器” * 第一个引发调用堆栈:

3 个答案:

答案 0 :(得分:0)

好像您正在从情节提要中实例化导航控制器。获取顶视图控制器。

@objc func buttonTapped(){

    let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
    if let top = viewController.topViewController {
        navigationController?.pushViewController(top, animated: true)
    }

    print("button tapped")
}

答案 1 :(得分:0)

这里似乎有很多项目是错误的。您没有将ViewController的{​​{1}}设置为rootViewController。您正在尝试推送另一个ViewController的新ViewController。这就是您所需要的。

首先:UISceneDelegate.swift

NavigationController

第二个:ViewController.swift

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

注意:SecondViewController的类型为UIViewController,而不是UINaviationViewController。

答案 2 :(得分:0)

在功能导航中,首先用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
}