Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch view controllers in swift?

Tags:

ios

swift

I'm trying to switch from one UIViewController to another using code. Right now I have,

self.presentViewController(ResultViewController(), animated: true, completion: nil)

All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?

like image 996
ThatHybrid Avatar asked Aug 19 '14 03:08

ThatHybrid


People also ask

How do I move between view controllers?

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below. in this you don't need any code to switch, it will switch on click of a button.

How do I present a two view controller in Swift?

For solving this, you will need to do a pretty simple trick which is to take a screenshot from the first view controller and passing it to the second view controller to display it while presenting the third view controller. You can check this repository to see how it is exactly could be done (Swift 3).


1 Answers

With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller and in the appropriate function type this:

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

self.navigationController.pushViewController(secondViewController, animated: true)

Without storyboard

let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
like image 104
Vivek Sehrawat Avatar answered Sep 18 '22 13:09

Vivek Sehrawat