Search code examples
swiftuinavigationcontrollerpushviewcontroller

UINavigationController won't pushViewController


I have two ViewControllers. One is called LoginVC which also is my rootviewcontroller, the other one is named SignUpVC.

In my AppDelegate I have set my UINavigationbar like this:

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow()
    window?.rootViewController = UINavigationController(rootViewController: LoginVC())

    return true
}

then in my LoginVC I use this to get my SignUpVC to show but it doesn't work.

@objc func handleShowSignUp() {
    let signUpVC = SignUpVC()
    navigationController?.pushViewController(signUpVC, animated: true)
}

Can someone please explain to me what I'm doing wrong?


Solution

  • you should use the File SceneDelegate, of this manner:

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow? // create Window
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            let LoginVC = ViewController() //create your viewController
            nav.pushViewController(contentView, animated: true) 
                   if let windowScene = scene as? UIWindowScene {
                       let window = UIWindow(windowScene: windowScene)
                       window.rootViewController = LoginVC
                       SceneDelegate.window = window
                       window.makeKeyAndVisible()
                   }
        }
    

    important: you can check the UIWindowScene