0

How to go back to rootview controller in swift 5 where scenedelegate is available instead of Appdelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
//        guard let _ = (scene as? UIWindowScene) else { return }
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController =
                storyboard.instantiateViewController(withIdentifier: "first_page")
            self.window!.rootViewController = initialViewController
            self.window!.makeKeyAndVisible()
        }
    }

this is my scenedelegate code need to dismiss/popback to my first page.

i am having the first page in Main.storyboard but my logout function is in another storyboard called Home.storyboard

tried

self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

and

self.navigationController?.popToRootViewController(animated: true)

But not working i have only one navigation controller that is for the first viewcontroller

Midhun Narayan
  • 829
  • 2
  • 9
  • 26

2 Answers2

0

Try this:-

let viewController = UIApplication.shared.windows.first!.rootViewController as! 
YourViewController

And then use this viewController

iOS Developer
  • 464
  • 6
  • 24
0

You can try to use topController for dismiss/popback or present/push:

    if var topController = UIApplication.shared.keyWindow?.rootViewController  {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
    }
Son Pham
  • 1,516
  • 1
  • 14
  • 22