Swift presentViewController

I programmatically have several View controllers in the iOS Swift Project. I do not have storyboards and would like to avoid them if possible. Is there a way to switch to another file viewcontroller.swift (we will call it view2.swift ) and be part of the function that the button calls?
I tried the following:

 let storyboard: UIStoryboard = UIStoryboard(name: "myTabBarName", bundle: nil) let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("myVCID") as UIViewController self.presentViewController(vc, animated: true, completion: nil) 

The above works with dropdowns, but I want another view2.swift to be called. It can be done?

+53
ios8 swift viewcontroller presentviewcontroller
Jun 07 '14 at 16:56
source share
11 answers

Try the following:

 let vc = ViewController() //change this to your class name self.presentViewController(vc, animated: true, completion: nil) 

With Swift3:

 self.present(vc, animated: true, completion: nil) 
+79
Jun 07 '14 at 16:59
source share

For those who get blank / black screens, this code worked for me.

 let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myVCId") as! MyVCName self.presentViewController(vc, animated: true, completion: nil) 

To set the "Identifier" to your VC, simply go to the Identity Inspector for the VC in the storyboard. Set the Storyboard ID to what you need for the ID. Take a look at the image below for reference.

+79
Oct 09 '14 at 19:40
source share

For reference, because this question is one of the first results of Google.

Breach of change in Swift 3:

The presentViewController method presentViewController replaced by the present method.

You can use it like old:

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

Example for opening the camera:

 let imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.camera imagePicker.allowsEditing = false self.present(imagePicker, animated: true, completion: nil) 
+19
Oct 06 '16 at 8:00
source share

Using Swift 2.1 +

  let vc = self.storyboard?.instantiateViewControllerWithIdentifier("settingsVC") as! SettingsViewController self.presentViewController(vc, animated: true, completion: nil) 

enter image description here

+6
Nov 03 '15 at 0:49
source share

For me, I had two views in two separate navigation controllers. I had to use a combination of the above.

 var vc = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeViewController") as! WelcomeViewController var navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil) 

Swift 3.x

  let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "VC-ID" as! yourViewController let navigationVC = UINavigationController(rootViewController: secondVC) self.present(navigationVC, animated: true, completion: nil) 
+5
Dec 13 '15 at
source share

Swift 3

 let vc = self.storyboard?.instantiateViewController(withIdentifier: "idMyViewControllerName") as! MyViewControllerName self.present(vc, animated: true, completion: nil) 
+5
Dec 09 '16 at 8:54
source share

Just use this: make sure with nibName otherwise the preloaded xib views will not be displayed:

var vc : ViewController = ViewController(nibName: "ViewController", bundle: nil) // change this to your class name

  self.presentViewController(vc, animated: true, completion: nil) 
+4
May 24 '15 at 3:30
source share

Solved the black screen by adding a navigation controller and installing the second view controller as rootVC.

 let vc = ViewController() var navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil 
+2
Mar 06 '15 at 23:56
source share

You can use the code below:

 var vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController; vc.mode_Player = 1 self.presentViewController(vc, animated: true, completion: nil) 
0
Sep 05 '15 at 8:24
source share

Another possibility is that you do not have the XIB included in your Build object (what happened to me).

This can happen if you have another goal for Dev, Test and Release Builds (which you should have anyway).

0
May 21 '16 at 22:18
source share

You can use the code:

 if let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController { let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.window?.rootViewController = vc } 
0
Oct 30 '17 at 3:42 on
source share



All Articles