Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Navigate from one View Controller to another using Swift

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]; 
like image 249
sathish Avatar asked Jun 04 '14 13:06

sathish


People also ask

How do I navigate between view controllers?

Control-drag from the button in the first view controller into the second view controller. In the dialog that pops up, select show. A top bar should appear in the second view controller and there should be a connection between our first view controller to our second view controller.


1 Answers

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) 
like image 166
Audrey Sobgou Zebaze Avatar answered Sep 22 '22 23:09

Audrey Sobgou Zebaze