i6x86 i6x86 - 4 months ago 47
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 '(() ->
Void)?'


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

Answer

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

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

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.