0

I am trying to present a second viewcontroller on top of the first at startup without dismissing the first. This post Swift 3 - loading multiple ViewControllers at launch certainly looks like it has the answer. It recommends:

Add in your main view controller

var secondViewController:UIViewController!

And in your viewDidLoad:

secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController

That's it. When you want to present it, use:

self.present(secondViewController, animated: true, completion: nil)  

This third line works great if, for example, I attach it as an action to a button. However, it does not work if it is in viewDidLoad: of the first viewController. This is what I need.

How can I automatically present the second viewController on top of the first viewController at launch?

picciano
  • 22,341
  • 9
  • 69
  • 82
Dan Sp.
  • 1,419
  • 1
  • 14
  • 21

4 Answers4

6

You need to do it at the proper place in your view controller. There are several methods that gets called when your view controller is presented, and they are meant for different tasks.

To present another view controller you should put it in viewWillAppear: or viewDidAppear:, as viewDidLoad: is too early in the presentation.

Read more about these methods here:

https://stackoverflow.com/a/5659007/4543629

LGP
  • 4,135
  • 1
  • 22
  • 34
  • you will never get back to the first viewcontroller if you always show second viewcontroller in `viewWillAppear` or `viewDidAppear` – user25 Mar 22 '19 at 22:34
  • @user25 it was not a requirement in the question. If you want to do that you can show the second view controller conditionally. – LGP Mar 23 '19 at 06:56
  • then how does a default calendar app implement this https://stackoverflow.com/questions/55315057/start-second-view-controller-of-navigation-stack-first? – user25 Mar 23 '19 at 15:01
1

I think this depends on what you are trying to do. If you want to push that ViewController at launch you could also try to present it from your AppDelegate in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool

Something like: (like i said it depends on your use case)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController
    let navigationController = UINavigationController(rootViewController: SecondViewController)
    self.window?.makeKeyAndVisible()
    self.window?.rootViewController?.present(navigationController, animated: false, completion: {() -> Void in
        mainMenuVC.tabBarControl = tabBarController
        })
    return true
}
Teetz
  • 3,475
  • 3
  • 21
  • 34
0

You can go to AppDelegate and setup window or if you workong with storyboard drag arrow to ViewControler what you want

David Kadlcek
  • 456
  • 1
  • 4
  • 18
0

Yes, that first comment by LGP does the trick. Thank you. I present it here as an answer. I added this function to the first viewController:

override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(true)
   self.present(secondViewController, animated: true, completion: nil)  
}

I did not look into the appropriateness of true or false in calling super.viewDidAppear(true) if anyone wishes to comment.

Dan Sp.
  • 1,419
  • 1
  • 14
  • 21