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.
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)
Programmatically navigate to another view controller/scene
I already found the answer
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
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)
How to go from one view controller in One storyboard to another viewcontroller in another storyboard programmatically?
Please follow below steps.
- Check your both view controllers StoryboardID (If Storyboard ID is already inserted then skip step 2).
- Insert View controller StoryboardID
Please check sample image of StoryboardID
Sample Code
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:"YourStoryBoardName" bundle:nil];
UIViewController *loginViewController = [storyBoard instantiateViewControllerWithIdentifier:"LoginViewController"];
// If Login View Controller is not a Navigation Controller then you need to create Navigation Controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self.navigationController pushViewController:viewController animated:true];
Related Topics
How to Disable the Back Navigation Menu in iOS 14+
Replace Occurrences of Nsnull in Nested Nsdictionary
Will iOS Region Monitoring Call Didenterregion on My App If My App Is in a Terminated State
Aes/Cbc/Pkcs5Padding in iOS Objective C Result Differs from Android
Xcode 6 Gm - Cllocationmanager
How to Use Call Directory Extension to Identify a Incoming Call in My Application
Custom Class Clusters in Swift
How to Determine the True Data Type of an Nsnumber
Eraser Not Working in iOS Drawing
Swiftui Landmarks App Tutorial Screen Navigates Back When Toggle Favorite
Application Not Automatically Launching in Device and Getting Error on Xcode
Add a Watermark on Video After Merging Video and Audio Asset into One in Swift3 iOS
iPhone Different Screen Sizes in Flash? (Getting Black Bars)
Presenting Uiviewcontroller from Skscene
What Is Most Common and Correct Practice to Get a Cgfloat from an Nsnumber
Swift Override Function in Extension
Two iOS Apps Using the Same Facebook App Id - Is It Possible