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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With