Presenting a view controller programmatically in swift
Do you want to present navController modally?
if yes, this is the answer
self.presentViewController(navController, animated: true, completion: nil)
"self" is the current view controller that will present the navController
And put it like this,
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var theButton = UIButton()
// Add the event to button
theButton.addTarget(self, action: "buttonTouchInside:", forControlEvents: .TouchUpInside)
self.view.addSubview(theButton)
}
func buttonTouchInside(sender:UIButton!)
{
// When the button is touched, we're going to present the view controller
// 1. Wrap your view controller within the navigation controller
let navController = UINavigationController(rootViewController: yourViewController)
// 2. Present the navigation controller
self.presentViewController(navController, animated: true, completion: nil)
}
}
But,
If you want to navigate between viewController in the navigationController, you can use
self.navigationController.pushViewController(viewControllerToPush, animated: true)
Swift programmatically navigate to another view controller/scene
Swift 5
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 you will be presenting like this:
newViewController.modalPresentationStyle = .fullScreen
This is the same for both programmatically created and storyboard created controllers.
Swift 3
With a programmatically created Controller
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
With a StoryBoard created Controller
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
programmatically present a new view controller in swift/programmatically give a class an Identifier
If you aren't using the storyboard you don't need an identifier. Just instantiate and present it.
let loadVC = SelectionScreen()
self.present(loadVC, animated: true, completion: nil)
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.
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."
If I present a ViewController programmatically without using a Navigation Controller, does the new VC replace the old one, or does it stack on top?
Here is a very simple, basic example...
The controllers are setup in Storyboard, each with a single button, connected to the corresponding @IBAction
.
The DOBViewController
has its Storyboard ID set to "dobVC".
The ThankYouViewController
has its Storyboard ID set to "tyVC".
MainVC
is embedded in a navigation controller (in Storyboard) and the navigation controller is set to Initial View Controller:
class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}
@IBAction func pushToDOB(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "dobVC") as? DOBViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
}
class DOBViewController: UIViewController {
@IBAction func pushToTY(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "tyVC") as? ThankYouViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
}
class ThankYouViewController: UIViewController {
@IBAction func popToRoot(_ sender: Any) {
navigationController?.popToRootViewController(animated: true)
}
}
Related Topics
Swift: Function with Default Parameter Before Non-Default Parameter
Dispatchqueue:Cannot Be Called with Ascopy = No on Non-Main Thread
Xcode Error: Missing Required Module 'Firebase'
Enumerateobjectsusingblock in Swift
Swift Calling Static Methods: Type(Of: Self) VS Explicit Class Name
Swift Struct Doesn't Conform to Protocol Equatable
Why Is There a Memory Leak at String Creation in Swift
How to Implement a Spritekit Timer
Adding Activity Indicator to Uialertview
Healthkit Error: Missing Com.Apple.Developer.Healthkit Entitlement
Swift Uisearchcontroller Wired Up in Core Data Project, App Runs, But Search Not Updating
How to Implement a Thread Safe Hashtable (Phonebook) Data Structure in Swift
Why Are Iboutlets Optionals After Swift 5 Migration
Swift Draw Shadow to a Uibezier Path
Migrating Data to App Groups Disables Icloud Syncing
Swift Await/Async - How to Wait Synchronously for an Async Task to Complete