'How to switch between viewСontrollers using pushViewController?
I have 3 controllers: MainViewController, FirstViewController, SecondViewController.
In SceneDelegate I have installed a rootViewController:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: winScene)
window?.rootViewController = MainViewController()
window?.makeKeyAndVisible()
}
How do I make the transition from FirstViewController to SecondViewController using pushViewController()?
FirstViewController:
@objc func buttonAction(sender: UIButton!) {
print(1)
let vc = SecondViewController()
navigationController?.pushViewController(vc, animated: true)
print(2)
}
The transition using present() works.
Solution 1:[1]
as @Rob said, at first you have to be using navigation controller within your current view controller hierarchy before you can use navigationController?.pushViewController
you have to doing like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let navController = UINavigationController()
let mainViewController = MainViewController()
navController.viewControllers = [mainViewController]
self.window!.rootViewController = navController
self.window?.makeKeyAndVisible()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mostafa Abbastabar |
