Instantiate and Present a Viewcontroller in Swift

Instantiate and Present a viewController in Swift

This answer was last revised for Swift 5.4 and iOS 14.5 SDK.


It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)

Make sure to set myVCID inside the storyboard, under "Storyboard ID."

Sample Image

Instantiate and present viewcontroller

You have a few options. I, like Ayaz, recommend doing this in a navigation controller but if for some reason you don't want to, I would recommend dismissing the SettingsVC when you are done with it. You can do this by calling self.dismiss(animated: true, completion: nil) after hitting the "close" or "back" button.

Swift 5. How present or show ViewController on button touch? Without Storyboard (programmatically)

First, using Storyboards

If you are working with storyboard. You should connect your button view storyboard.

@IBAction fileprivate func handlePresentingView(_ sender: UIButton) {
let vc = SecondVC()
present(vc, animated: true, completion: nil)
}

Second, programmatically

1: If you are working programmatically
in viewDidLoad add the following line.

    mybutton.addTarget(self, action: #selector(handlePresentingVC(_:)), for: .touchUpInside)

2: Your action method

  @objc func handlePresentingVC(_ sender: UIButton) {
let vc = SecondVC()
present(vc, animated: true, completion: nil)
}

In my example, I'm assuming that you don't have a storyboard file for
your SecondVC view controller.

If SecondVC is connected to a storyboard view controller, you will
need to change the instantiation of your secondVC object inside of
your button's action method.

1: Select your SecondVC's view controller in your storyboard.

2: Add a Storyboard ID to it.
Sample Image

3: Change your button's action method to the following.

  @objc func handlePresentingVC(_ sender: UIButton) {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let secondVc = storyboard.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC

present(secondVc, animated: true, completion: nil)
}

In Swift 5 and iOS 13

The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.

To retain the old style you need to modify the view controller inside of your button's action method like this:

secondVc.modalPresentationStyle = .fullScreen

This is the same for both programmatically created and storyboard created controllers.

How to modally present a ViewController from SceneDelegate?

You can't present a UIViewController (modally or fullscreen) which is window's rootViewController.

To show Onboarding screen when user open app for the first time, set Home Screen/Login Screen (or any screen you want) as rootViewController in SceneDelegate. Then from that UIViewController you can check if user open app for first time or not. Depending on that you can show the Onboarding screen.

You must embed the rootViewController in a UINavigationViewController to present/push another UIViewController

In SceneDelegate.swift modify the code like below.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
//embed in UINavigationController
let navController = UINavigationController(rootViewController: controller)
window?.rootViewController = navController
window?.makeKeyAndVisible()
}

Then in the Home screen (i assume it as HomeViewController) check the status of first opening and present Onboarding screen.

class HomeViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "OnboardingViewController") as! OnboardingViewController
controller.modalPresentationStyle = .formSheet
self.present(controller, animated: true)
}
}

You can get the rootViewController from anywhere in the app by using the code below. And then present any ViewController you want to show.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "OnboardingViewController") as! OnboardingViewController
controller.modalPresentationStyle = .formSheet
let rootVC = UIApplication.shared.windows.first!.rootViewController
rootVC?.present(controller, animated: true)


Related Topics



Leave a reply



Submit