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."
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
yourSecondVC
view controller.If
SecondVC
is connected to a storyboard view controller, you will
need to change the instantiation of yoursecondVC
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.
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
React-Native, How to Get File-Asset Image Absolute Path
How to Create Custom Calendar in React Native
How to Change Status Bar Text Color in Ios
Set the Maximum Character Length of a Uitextfield
Swift: Extra Argument 'Error' in Call
How to Change the Push and Pop Animations in a Navigation Based App
Undefined Symbols For Architecture I386: _Objc_Class_$_Skpsmtpmessage", Referenced From: Error
How to Get My Ip Address Programmatically on Ios/Macos
Push Notification Issue With iOS 10
Handling Applicationdidbecomeactive - "How Can a View Controller Respond to the App Becoming Active"
How to Get the Root Directory of My iOS App
Best Way to Check If Object Is Out of Bounds in Array
Determine Device (Iphone, Ipod Touch) With Ios
How to Exit Iphone Application
Status Bar and Navigation Bar Appear Over My View'S Bounds in iOS 7
How to Create a Basic Uibutton Programmatically