18

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?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
ThatHybrid
  • 383
  • 2
  • 3
  • 9

8 Answers8

24

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)
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
  • 3
    I don't want it to start a new intent activity; I just need it to literally switch the views, because I have an intro text in the beginning which never ever needs to be displayed again, and I don't want them able to "back" to it. So it needs to be permanently replaced by the table view. – pete Jul 14 '17 at 17:46
  • 1
    What is `NibName`? – Andremoniy Jun 05 '18 at 17:37
8

You can try this one

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController

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

Make sure you set your view controller identifier.

enter image description here

Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • I don't want it to start a new intent activity; I just need it to literally switch the views, because I have an intro text in the beginning which never ever needs to be displayed again, and I don't want them able to "back" to it. So it needs to be permanently replaced by the table view. – pete Jul 14 '17 at 17:47
  • What if you want the ResultView to reload as part of the switch to it? – John Jul 25 '21 at 18:45
4

Swift 3

To present a controller modally

let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)

To show a controller

self.show(vc, sender: self)
Cyril
  • 2,783
  • 1
  • 24
  • 35
2

Try this:

let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
2

The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:

self.performSegueWithIdentifier("segue", sender: nil)

this will trigger the segue that will go to your resultViewController

William DeGroot
  • 71
  • 1
  • 1
  • 6
0

The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen. Make sure you are using a UINavigationViewController.

Source: ios UINavigationController Tutorial.

Shaba Aafreen
  • 1,497
  • 2
  • 8
  • 4
0

Shaba's answer is a good start but requires the following so that you can control the segue from within your view controller:

override func shouldPerformSegue(withIdentifier identifier: String, 
              sender: Any?) -> Bool {    
 // your code here    
 // return true to execute the segue    
 // return false to cause the segue to gracefully fail  } 

Source

picciano
  • 22,341
  • 9
  • 69
  • 82
0

vc = YourViewController()

UIApplication.shared.keyWindow?.rootViewController = vc

Or

vc = YourViewController()

UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC

Hussein
  • 95
  • 1
  • 1
  • 5
  • It might be obvious to you but can you elaborate? – loa_in_ Nov 28 '20 at 01:40
  • vc = YourViewCotroller() //the VC you want to navigate to – Hussein Dec 17 '20 at 19:00
  • UIApplication.shared.keyWindow //here you are telling the compiler that you want to handle the root/main window by code then you will type .rootViewController // means the ViewController That you to make As the startUp/Destination ViewController – Hussein Dec 17 '20 at 19:06