0

I have two ViewControllers. One is called LoginVC which also is my rootviewcontroller, the other one is named SignUpVC.

In my AppDelegate I have set my UINavigationbar like this:

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow()
    window?.rootViewController = UINavigationController(rootViewController: LoginVC())

    return true
}

then in my LoginVC I use this to get my SignUpVC to show but it doesn't work.

@objc func handleShowSignUp() {
    let signUpVC = SignUpVC()
    navigationController?.pushViewController(signUpVC, animated: true)
}

Can someone please explain to me what I'm doing wrong?

6
  • Does the LoginVC display? Not sure if it is just a typo but you also need to add window.makeKeyAndVisible(). Besides that it looks about right. Commented Dec 23, 2019 at 15:47
  • Check if LoginVC has navigationController Commented Dec 23, 2019 at 15:58
  • 1
    did you remove storyboard from main storyboard on your .plist ? The lack of self.window?.makeKeyAndVisible() on your appdelegate makes me thing like you are opening default storyboard initial VC, but you think you opened it from didFinishLaunchingWithOptions. Commented Dec 23, 2019 at 16:03
  • Thanks for the reply guys! I added window.makeKeyAndVisible() and also removed "Main" from my .plist but it still won't work. Commented Dec 23, 2019 at 16:25
  • @MattiasTörnqvist my answer have worked ? Commented Dec 23, 2019 at 16:36

3 Answers 3

1

you should use the File SceneDelegate, of this manner:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow? // create Window

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let LoginVC = ViewController() //create your viewController
        nav.pushViewController(contentView, animated: true) 
               if let windowScene = scene as? UIWindowScene {
                   let window = UIWindow(windowScene: windowScene)
                   window.rootViewController = LoginVC
                   SceneDelegate.window = window
                   window.makeKeyAndVisible()
               }
    }

important: you can check the UIWindowScene

Sign up to request clarification or add additional context in comments.

1 Comment

I'm doing this for iOS 12, it's part of a tutorial.
0

Directly initializing LoginVC() and SignUpVC() is kind of wrong. Try something like this if you are using storyboards

let storyboard = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let nav = storyboard.instantiateInitialViewController() //if you want the initial one

let storyBoard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let someVC: SomeViewController = giftStoryBoard.instantiateViewController(withIdentifier: "SomeViewController") as! SomeViewController //don't forget to set identifier on interfacebuilder

If you are using only .xib

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)

then try to pushing it to navigation controller

1 Comment

I'm not using Storyboards, trying to do it programmatically.
0

This did the trick in the SceneDelegate-file:

    @available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let scene = scene as? UIWindowScene else { return }
    window = UIWindow(windowScene: scene)
    window?.rootViewController = UINavigationController(rootViewController: LoginVC())
    window?.makeKeyAndVisible()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.