83

I programatically have multiple View Controllers in an iOS Swift Project. I do not have the storyboards and would like to avoid them if possible. Is there a way to switch to another viewcontroller.swift file (We will call it view2.swift) and have it be part of a function that a button calls?
I have 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 storyboards, but I want another view2.swift to be called. Can this be done?

Karthik Kumar
  • 1,375
  • 1
  • 12
  • 29
Z-Tech
  • 4,360
  • 4
  • 17
  • 10

14 Answers14

130

Try this:

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)
Julien Fouilhé
  • 2,583
  • 3
  • 30
  • 56
Adam
  • 26,549
  • 8
  • 62
  • 79
  • 3
    I'd recommend using the initializer: `ViewController(nibNameOrNil: nil, bundleOrNil: nil)` – Simon Germain Jun 07 '14 at 17:01
  • Thank you! This ended up working. It is also much simpler than the old way to do it in Objective-C, which I guess was the whole point of swift. Thanks again! – Z-Tech Jun 08 '14 at 15:41
  • 23
    @Adam, I am doing exactly same to show view on a button click. It shows up black blank screen. There are no errors. Any idea why am I getting black screen? – Sanjivani Jun 11 '14 at 06:57
  • 3
    @Sanjivani, From iOS7, the default color of the view is clear color, So it show black screen. Set color in VIewController's viewDidLoad as override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.greenColor() } – Shanmugaraja G Apr 20 '15 at 20:47
  • 4
    this will occur blank screen ! – Mojtaba Yeganeh May 16 '15 at 18:21
  • @Sanjivani you have explicitly type the nib file name, so instead of "let vc = ViewController()" type "let vc = ViewController(nibName: "ViewController", bundle: nil)". This will solve your problem. – Rezoan Aug 20 '15 at 14:29
  • 1
    How is this accepted and vote up so many times, its not even using initializer for viewcontroller, do like this will result in nothing – Tj3n Dec 26 '16 at 04:13
  • Value of type 'AppDelegate' has no member 'present' – J A S K I E R Jul 28 '18 at 16:10
  • @Oleksandr `AppDelegate` is not a view controller. You could try `self.rootViewController.present( ...` but this can depend on your use case and your app architecture. – Adam Jul 29 '18 at 19:32
  • Totally wrong approach, with this method, you only create a new ViewController instance not VC in the storyboard. – Onder OZCAN May 21 '19 at 16:03
  • @OnderOZCAN this is exactly right approach when you don’t use storyboards – Adam May 21 '19 at 16:04
  • 1
    Oh sorry I missed the title "Programmatically" my bad :( – Onder OZCAN May 21 '19 at 16:17
  • If you get a black screen is because you are not instantiating your controller form a xib/nib/storyboard – Bruno Berisso Oct 31 '19 at 02:15
112

For those getting blank/black screens this code worked for me.

    let vc = self.storyboard?.instantiateViewController(withIdentifier: myVCID) as! myVCName
    self.present(vc, animated: true, completion: nil)

To set the "Identifier" to your VC just go to identity inspector for the VC in the storyboard. Set the 'Storyboard ID' to what ever you want to identifier to be. Look at the image below for reference.

Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
boidkan
  • 4,691
  • 5
  • 29
  • 43
21

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

Breaking change in Swift 3:

The method presentViewController is replaced by the method present.

You can use it like the old one:

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

Example to open the camera:

let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
Arenzel
  • 1,156
  • 1
  • 11
  • 18
19

Swift 3 and Swift 4

let vc = self.storyboard?.instantiateViewController(withIdentifier: "idMyViewControllerName") as! MyViewControllerName
self.present(vc, animated: true, completion: nil)
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Kaptain
  • 1,358
  • 12
  • 20
8

For me, I had two views in two separate nav 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)
Mathi Arasan
  • 869
  • 2
  • 10
  • 32
Keith Holliday
  • 1,692
  • 21
  • 15
6

Using Swift 2.1+

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

enter image description here

Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
4

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

let vc = ViewController()       
var navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil
AMG
  • 41
  • 3
4

Just use this : Make sure using nibName otherwise preloaded views of xib will not show :

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

 self.presentViewController(vc, animated: true, completion: nil)
Alok
  • 24,880
  • 6
  • 40
  • 67
1

You don't need to instantiate the ViewController in Storyboard just to get present() ViewController to work. That's a hackish solution.

If you see a black/blank screen when presenting a VC, it might be because you're calling present() from viewDidLoad() in the First/RootViewController, but the first View isn't ready yet.

Call present() from viewDidAppear to fix this, i.e.:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let yourVC = YourViewController()
    self.present(yourVC, animated: true, completion: nil)
}

Once any "View" has appeared in your App, you can start calling present() from viewDidLoad().


Using UINavigationController (as suggested in an answer) is another option, but it might be an overkill to solve this issue. You might end up complicating the user flow. Use the UINavigationController based solution only if you want to have a NavigatonBar or want to return to the previous view controller.

Nitin Nain
  • 5,113
  • 1
  • 37
  • 51
1

It's already answered. But adding one more thing while presenting a UIViewController, If anyone is trying to add UIModalPresentationStyle :

if directly presenting the UIViewController as fullScreen:

let viewController = UIViewController()
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true)  

If there is UINavigationController with root view controller as UIViewController:

let viewController = UIViewController()
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true)

More helpful answers:

Amit
  • 4,837
  • 5
  • 31
  • 46
0

You can use below code :

var vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController;
            vc.mode_Player = 1
            self.presentViewController(vc, animated: true, completion: nil)
Amarjit
  • 4,327
  • 2
  • 34
  • 51
0

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

This could happen if you have a different target for Dev, Test & Release Builds (which you should have anyway).

Gerard
  • 4,818
  • 5
  • 51
  • 80
0

I had a similar issue but in my case, the solution was to dispatch the action as an async task in the main queue

DispatchQueue.main.async {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: myVCID) as! myVCName
    self.present(vc, animated: true, completion: nil)
}
de Isaac
  • 1
  • 1
  • 1
-1

You can use code:

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController {
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = vc
}
tvtruong
  • 99
  • 1
  • 4