-
-
Save alexpaul/875d1c8ce45a5f536d0c81087285f4d8 to your computer and use it in GitHub Desktop.
// accessing the window and changing the root view controller property | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// getting access to the window object from SceneDelegate | |
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | |
let sceneDelegate = windowScene.delegate as? SceneDelegate | |
else { | |
return | |
} | |
let viewcontroller = UIViewController() | |
viewcontroller.view.backgroundColor = .blue | |
sceneDelegate.window?.rootViewController = viewcontroller | |
} | |
// extension for resetting the window on a UIKit application | |
extension UIViewController { | |
private func resetWindow(with vc: UIViewController?) { | |
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { | |
fatalError("could not get scene delegate ") | |
} | |
sceneDelegate.window?.rootViewController = vc | |
} | |
func showViewController(with id: String) { | |
let vc = storyboard?.instantiateViewController(identifier: id) | |
resetWindow(with: vc) | |
} | |
} | |
// example case of resetting the window from a login view controller | |
// this will transition from a login vc to a navigation controller with the app logic | |
@IBAction func explore(_ sender: UIButton) { | |
showViewController(with: "BBQNavController") | |
} |
@alexpaul
Value of type 'SceneDelegate' has no member 'window'
Error with code code
// extension for resetting the window on a UIKit application
extension UIViewController {
private func resetWindow(with vc: UIViewController?) {
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {
fatalError("could not get scene delegate ")
}
sceneDelegate.window?.rootViewController = vc
}
func showViewController(with id: String) {
let vc = storyboard?.instantiateViewController(identifier: id)
resetWindow(with: vc)
}
}
error
@2020KamelAlaghbari, window
isn't in the UISceneDelegate prototype, but when I create a new project (iOS, Storyboard) using Xcode 13.4.1, the project includes a class called SceneDelegate, that conforms to UIWindowSceneDelegate, that includes a var window: UIWindow?.
@DG0BAB