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)
Share:
261,840
sathish
Author by

sathish

Updated on July 08, 2022

Comments

  • sathish
    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
    Sanjivani almost 10 years
    @audrey,Hi, how to set navigationController?
  • Audrey Sobgou Zebaze
    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
    Kokodoko almost 10 years
    How do you assign the first view controller as the "rootViewController" ?
  • Audrey Sobgou Zebaze
    Audrey Sobgou Zebaze almost 10 years
    Are you using storyboard ? what kind of project template do you have ?
  • Raghavendra
    Raghavendra over 9 years
    I 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
    Morkrom over 9 years
    I am having an issue with the animated: parameter like so: "Cannot convert the expression's type '$T7??' to type 'BooleanLiteralConvertible'".
  • kakubei
    kakubei about 9 years
    You need to make sure your controllers are embedded in a NavigationController for this to work, otherwise you will get errors.
  • Audrey Sobgou Zebaze
    Audrey Sobgou Zebaze over 8 years
    @AkashRaghani did you set the Storyboard ID in Storyboard -> identity inspector ?
  • Francis Reynolds
    Francis Reynolds over 8 years
    This is because your view controller wasn't ebbed in a Navigation View Controller.
  • Abhishek Thapliyal
    Abhishek Thapliyal about 8 years
    unexpectedly found nil while unwrapping an Optional value
  • mfaani
    mfaani over 7 years
    Why have you removed the ? from self.storyboard? don't you need to unwrap it?
  • JJJ
    JJJ over 6 years
    Could you please explain shortly what your code does, why it solves the problem and how it's different from all the other answers.
  • Admin
    Admin over 6 years
    here i am using storyboard id as identifier . Via the reference (objViewController) push the control to the View Controller class
  • SPatel
    SPatel over 5 years
    i think it's not Swift or Objc syntax
  • Grenoblois
    Grenoblois about 4 years
    type of segue ?
  • vikingosegundo
    vikingosegundo almost 3 years
    you know, Swift is on version 5.4.