Search code examples
swiftuinavigationcontroller

Naviagtion Controller not working after chnaging main storyboard in Scene delegate


I have deleted the main.storyboard from my project and changed the initial screen to Home.storyboard using scenedelegate.swift file. Now when I try to Navigation controller, it is nil.

SceneDelegate.swift

guard let winScene = (scene as? UIWindowScene) else { return }        
window = UIWindow(windowScene: winScene)        
let storyboard = UIStoryboard(name: "VerifyOTP", bundle: nil)        
let initialViewController = storyboard.instantiateViewController(identifier: "VerifyOTP")        window?.rootViewController = initialViewController        
window?.makeKeyAndVisible()

HomeVC.swift

let OrderScreen: PlaceOrderVC = self.storyboard!.instantiateViewController(withIdentifier: "PlaceOrder") as! PlaceOrderVC            
let navigationController = self.navigationController!            
print(navigationController)            
OrderScreen.BillDisplay = self.billing            
navigationController.pushViewController(OrderScreen, animated: true)

Solution

  • You need to embed your initialViewController in navigation controller and then make the navController the rootViewController. Check the code below.

        guard let winScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: winScene)
        let storyboard = UIStoryboard(name: "VerifyOTP", bundle: nil)
        let initialViewController = storyboard.instantiateViewController(identifier: "VerifyOTP")
        let navController = UINavigationController(rootViewController: initialViewController)
        window?.rootViewController = navController
        window?.makeKeyAndVisible()