i6x86 i6x86 - 1 year ago 119
Swift Question

How to properly dismiss modal viewcontroller with completion handler

i have viewController1 and viewController2 which is modaly presented and i want to use the completion handler when i dismiss the 2nd, but i cant get the implementation. i thought that i have to write a function and just put it there like:

viewController.dismissViewControllerAnimated(true, completion: funcToCall())

but then i get this error:

Cannot convert value of type '()' to expected argument type '(() ->

anyone can explain me, how can i execute properly the completion handler, please?

Answer Source

Pretty easy, pass funcToCall() as a completion parameter (important note - I'm using curly braces here):

viewController.dismissViewControllerAnimated(true, completion: { 

A completion parameter documentation:

The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter.