0Day Forums
Swift presentViewController - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Swift (https://0day.red/Forum-Swift)
+--- Thread: Swift presentViewController (/Thread-Swift-presentViewController)

Pages: 1 2


Swift presentViewController - boppers926231 - 07-18-2023

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?<br>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?


RE: Swift presentViewController - Propolder719 - 07-18-2023

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

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:

-

[To see links please register here]



[1]:

[To see links please register here]




RE: Swift presentViewController - Drhoran7 - 07-18-2023

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)
}


RE: Swift presentViewController - grimacing904217 - 07-18-2023

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.
![](
)


RE: Swift presentViewController - raptril584032 - 07-18-2023

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.


RE: Swift presentViewController - jonnycakes731845 - 07-18-2023

**Swift 3 and Swift 4**

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


RE: Swift presentViewController - removable942 - 07-18-2023

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
}


RE: Swift presentViewController - analyticities333906 - 07-18-2023

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)


RE: Swift presentViewController - raymonraymond961 - 07-18-2023

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)


RE: Swift presentViewController - enteron304475 - 07-18-2023

*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)