Set Window Root View Controller

With this short code example I am going to share with you how to set current window root view controller to a different one. The effect is as if you are replacing the current view controller with a new one. The new view controller will not be pushed into navigation stack and will not be presented on top of the window it will take place of existing view controller and very much looks like you are replacing current view controller with a new one. I use this approach whenever I need to log user out and take them to page #1.

The below code example needs to be ran from a ViewController rather than from AppDelegate.swift file for example.

Set Window Root View Controller Code Example in Swift

let signInPage = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = signInPage

Checkout the below video courses to learn more about Mobile App Development for iOS platform with Swift.

[raw_html_snippet id=”cookbookpagecoursesheader”]

Unit Testing Swift Mobile App

Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.