How to navigate from one view controller to another in iOS?

iOSMobile DevelopmentiPhone/iPad

To navigate from one view Controller to another view Controller in iOS, we need to use Navigation controller. Navigation controller manages a stack of View controller when we go from one view to another view.

Navigation from one view controller to another view controller can be done like mentioned below.

Step 1 − Create a View controller object.

let vc = self.storyboard?.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController

In this step we initialize an object of the type of our another view controller, to which we want to navigate. The identifier variable should be same as the identifier of our second view controller.

Step 2 − Navigating to Other View Controller

self.navigationController?.pushViewController(vc, animated: true)

In this step we navigate to the second view controller with help of our navigation controller. Here we are pushing the View controller. We can also present the other view controller instead of pushing it.

self.present(vc, animated: true, completion: nil)

When we run the above code on device we get the following result.

raja
Published on 21-Jan-2019 12:07:55
Advertisements