简体   繁体   中英

Dismiss a modal ViewController after X seconds

I have a login modal UIViewController open and what to show a second "Thank you" Viewcontroller modally for 60 seconds and then dismiss it, but I can figoure out how to do this in SWIFT. (It was easy in Objectiv C)

Here is my code for opening the "Thank you viewcontroller" after closing the first one.

        weak var pvc = self.presentingViewController
    self.dismiss(animated: true, completion: {
        let thankYouExistingVC = self.storyboard?.instantiateViewController(identifier: "ThankYouExistingVC") as! ThankYouExistingVC
        pvc?.present(thankYouExistingVC, animated: true, completion: nil)
    })

You can do it using DisaptchQueue :

    DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
        // Do whatever you want
        theViewControllerToDismiss.dismiss(animated:true, completion: nil)
    }

I dont know why, but I always seem to find a way just after i ask the question here, no matter how long i have been searching. This code worked for me. Sorry about that

For people that search the same as me. This code closes the current viewcontroller and open a new one for 5 seconds before it closes it again.

Thank you all for trying to help <3

        weak var pvc = self.presentingViewController
    self.dismiss(animated: true, completion: {
                let thankYouExistingVC = self.storyboard?.instantiateViewController(identifier: "ThankYouExistingVC") as! ThankYouExistingVC

            pvc?.present(thankYouExistingVC, animated: true, completion: {

                    Timer.scheduledTimer(withTimeInterval: 5, repeats: false, block: {_ in
                        thankYouExistingVC.dismiss(animated: true) {

                        }
                    })
                })

        }
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM