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?

8 Answers 8

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
What is NibName?
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

2 Comments

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.
What if you want the ResultView to reload as part of the switch to it?
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)

Comments

2

Try this:

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

Hope this helps.. :)

1 Comment

You should add some sort of explanation as to why this solves the problem. Also, what if OP is using storyboards?
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

Comments

1

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

3 Comments

It might be obvious to you but can you elaborate?
vc = YourViewCotroller() //the VC you want to navigate to
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
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.