Byte Byte - 1 year ago 124
Swift Question

How to dismiss viewController in ios using Swift

I have a viewController which contains only an imageview. I want to present it whenever there is a loading in the application like fetching data from a webservice. So I have created a function in my loaderViewController as

func showLoading(viewController:UIViewController) {
viewController.presentViewController(LoadingViewController(), animated: false, completion: nil)

This is working as expected, when I call this function when desired like below

var loader = LoadingViewController()

It show me the viewController with image.

But Now want to dismiss this viewController when desired but I am not able to do so, This is what I have tried so far, I created another function in my LoaderViewController as

func dismissLoader() {
let load = LoadingViewController()
load.dismissViewControllerAnimated(true) {
print("Dismissing Loader view Controller")

But its not working and the viewController is not disappering from the screen.

Please guide me

Answer Source

You don't have to create a new instance of your loader and call dismissViewControllerAnimated(_:Bool) on it.

Just call


on your viewController

So, your function will be

func dismissLoader() {

    dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download