How to Navigate from one View Controller to another using Swift
261,840
Solution 1
Create a swift file (SecondViewController.swift) for the second view controller and in the appropriate function type this:
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Swift 2+
let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)
Swift 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
Solution 2
In my experience navigationController
was nil so I changed my code to this:
let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)
Don't forget to set ViewController StoryBoard Id
in StoryBoard
-> identity inspector
Solution 3
If you don't want the back button to appear (which was my case, because I wanted to present after a user logged in) here is how to set the root of the nav controller:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
Solution 4
SWIFT 3.01
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC
self.navigationController?.pushViewController(secondViewController, animated: true)
Solution 5
In swift 4.0
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier")
var navi = UINavigationController(rootViewController: viewController!)
navigationController?.pushViewController(navi, animated: true)
Author by
sathish
Updated on July 08, 2022Comments
-
sathish almost 2 years
I'd like to navigate from one view controller to another. How can I convert the following Objective-C code into Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController]; [self.navigationController pushViewController:navi animated:YES];
-
Sanjivani almost 10 years@audrey,Hi, how to set navigationController?
-
Audrey Sobgou Zebaze almost 10 years@Sanjivani, Hi your view controller can be created with Storyboard and assign your firstViewController as rootViewController.Your navigation Controller (swift) class will look like this:
import UIKit
class SwiftNavigationController: UINavigationController { init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Custom initialization }`` override func viewDidLoad() { super.viewDidLoad() }
-
Kokodoko almost 10 yearsHow do you assign the first view controller as the "rootViewController" ?
-
Audrey Sobgou Zebaze almost 10 yearsAre you using storyboard ? what kind of project template do you have ?
-
Raghavendra over 9 yearsI also had same problem, I was stuck at how to navigate from one ViewController to another. When I tried this code, I am getting this error:
unexpectedly found nil while unwrapping an Optional value
at second line of your code. Please help -
Morkrom over 9 yearsI am having an issue with the animated: parameter like so: "Cannot convert the expression's type '$T7??' to type 'BooleanLiteralConvertible'".
-
kakubei about 9 yearsYou need to make sure your controllers are embedded in a NavigationController for this to work, otherwise you will get errors.
-
Audrey Sobgou Zebaze over 8 years@AkashRaghani did you set the Storyboard ID in Storyboard -> identity inspector ?
-
Francis Reynolds over 8 yearsThis is because your view controller wasn't ebbed in a Navigation View Controller.
-
Abhishek Thapliyal about 8 yearsunexpectedly found nil while unwrapping an Optional value
-
mfaani over 7 yearsWhy have you removed the
?
fromself.storyboard
? don't you need to unwrap it? -
JJJ over 6 yearsCould you please explain shortly what your code does, why it solves the problem and how it's different from all the other answers.
-
Admin over 6 yearshere i am using storyboard id as identifier . Via the reference (objViewController) push the control to the View Controller class
-
SPatel over 5 yearsi think it's not Swift or Objc syntax
-
Grenoblois about 4 yearstype of segue ?
-
vikingosegundo almost 3 yearsyou know, Swift is on version 5.4.