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.
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)
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)
}
}
How to present a view controller programmatically
First you need to create an action for your button, normally via Ctrl
+drag
. On that action implementation you should alloc the destination ViewController. The code would be something like this:
- (IBAction)buttonTapped {
MyViewController *modalViewController = [MyViewController new];
[self presentViewController:modalViewController animated:YES completion:nil];
}
Since you said that your file is a nib, I'm assuming that you aren't on a storyboard with both ViewControllers on it and the ViewController to be presented is code based. If this isn't the case, please elaborate your question.
As requested: Loading a VC from a storyboard
- (IBAction)buttonTapped {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
RSSViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RSSVC"];
// OR If is initial
// RSSViewController *viewController = [storyboard instantiateInitialViewController];
viewController.feedURL = @"...";
[self presentViewController:viewController animated:YES completion:nil];
}
If you need, ViewController identifier should be set in the Identity Inspector in the Storyboard.
Presenting modal in iOS 13 fullscreen
With iOS 13, as stated in the Platforms State of the Union during the WWDC 2019, Apple introduced a new default card presentation. In order to force the fullscreen you have to specify it explicitly with:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
How to call a View Controller programmatically?
To create a view controller:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
Loading a view controller from the storyboard:
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier
of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
Related Topics
iOS Certificate Pinning with Swift and Nsurlsession
How to Get All Enum Values as an Array
Frosted Glass (iOS 7 Blur) Effect
How to Convert Nsinteger to Nsstring Datatype
Present Uialertcontroller on Top of Everything Regardless of the View Hierarchy
Any Way to Print in Color with Nslog
Module Was Not Compiled for Testing' When Using @Testable
Watchkit Appicon - the App Icon Set Named "Appicon" Did Not Have Any Applicable Content
Set Uitableview Content Inset Permanently
iOS App Submission and Beta Review Process
Launch a Local Notification at a Specific Time in iOS
Using Convertpoint to Get the Relative Position Inside a Parent Uiview
Logging Method Signature Using Swift
Find Where Object Is Retained with Arc
How to Show Uipickerview When Selecting Uitextfield