Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to present UIViewController while a presentation is in progress!-Warning

Assume a new iOS project, just with a navigation controller (correctly wired as entry point) and an overridden viewDidAppear() containing the following three lines of code:

            self.presentViewController(UIViewController(), animated: true, completion: nil)
            self.dismissViewControllerAnimated(true, completion: {})
            self.presentViewController(UIViewController(), animated: true, completion: nil)

When executed, that code will raise a warning "Attempt to present UIViewController while a presentation is in progress!" when attempting to present the second controller.

Question: What exactly am I missing in order to dismiss the controller correctly before calling another controller?

like image 343
Basti Avatar asked Feb 18 '26 20:02

Basti


1 Answers

You'll need to add some sort of delay on that initial presentViewController call as illustrated below:

override func viewDidAppear(animated: Bool) {
    presentViewController(UIViewController(), animated: true) { () -> Void in
        self.delay(0.1, closure: { () -> () in
            self.dismissViewControllerAnimated(true, completion: nil)
        })
    }
}


func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

It seems the completion block is called before the animation is truly complete.

like image 147
Aaron Bratcher Avatar answered Feb 20 '26 12:02

Aaron Bratcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!