Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
loader.showLoading(self)

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

like image 792
Byte Avatar asked Nov 29 '22 09:11

Byte


1 Answers

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

Just call

self.dismissViewControllerAnimated(true)

on your viewController

So, your function will be

func dismissLoader() {

    dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller")
    }
}
like image 155
Luca D'Alberti Avatar answered Dec 05 '22 14:12

Luca D'Alberti