4

I have viewController1 and viewController2 which is modaly presented and I want to use the completion handler when I dismiss the 2nd, but I can't 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?

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
i6x86
  • 1,557
  • 6
  • 23
  • 42

2 Answers2

15

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.

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
  • then the problem were i wasn't using the curly brackets? however, it works but it seems only partially. when i try to present a new viewController i still get this error: Attempt to present viewController3 whose view is not in the window hierarchy! but then somehow it presents it. it drives me nuts – i6x86 Jul 26 '16 at 17:25
  • i'll try to explain it better. i'm dismissing the vc2 with completion: notification and observer in vc1 that triggers segueWithIdentifier when the vc2 is supposedly dismissed, but then i was getting the "Attempt to present viewController3 whose view is not in the window hierarchy!" error. now i'm still getting it but then the segue is executed and i'm not sure if this could bring future issues. – i6x86 Jul 26 '16 at 17:29
  • Oh, this is kinda of another error. Please check this thread - http://stackoverflow.com/questions/26022756/warning-attempt-to-present-on-whose-view-is-not-in-the-window-hierarchy-s – Evgeny Karkan Jul 26 '16 at 17:31
  • i thought the problem appears due to the vc2 wasn't fully dismissed. – i6x86 Jul 26 '16 at 17:34
  • i'm not sure that this tread http://stackoverflow.com/questions/26022756/warning-attempt-to-present-on-whose-view-is-not-in-the-window-hierarchy-s can apply to this issue because when i change the way i'm presenting the VC i.e modal presentation, i'm getting this warning message: "Warning: Attempt to present vc3 on vc1 while a presentation is in progress!" should i post a new question explaining the issue? edit: that's why i think the problem is vc2 is still active when the vc3 is presenting – i6x86 Jul 26 '16 at 19:14
  • Does funcToCall exists with the same view controller – Umesh Sharma Sep 26 '18 at 11:36
  • This is still true in 2020 with Swift 5.2 – iosCurator Dec 22 '20 at 03:19
3

Just remove the brackets and pass the function name as parameter like this:

viewController.dismissViewControllerAnimated(true, completion: funcToCall)

This works as long as your function funcToCall is of type ()->Void like

func funcToCall() {  
    // do something
}
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54