Is it possible to call a block completion handler from another function in iOS?

Erken

I have a custom UIView with a UITapGestureRecognizer attached to it. The gesture recognizer calls a method called hide() to remove the view from the superview as such:

func hide(sender:UITapGestureRecognizer){
    if let customView = sender.view as? UICustomView{
        customView.removeFromSuperview()
    }
}

The UICustomView also has a show() method that adds it as a subview, as such:

func show(){
    // Get the top view controller
    let rootViewController: UIViewController = UIApplication.sharedApplication().windows[0].rootViewController!!
    // Add self to it as a subview
    rootViewController.view.addSubview(self)
}   

Which means that I can create a UICustomView and display it as such:

let testView = UICustomView(frame:frame) 
testView.show() // The view appears on the screen as it should and disappears when tapped

Now, I want to turn my show() method into a method with a completion block that is called when the hide() function is triggered. Something like:

testView.show(){ success in
    println(success) // The view has been hidden
}

But to do so I would have to call the completion handler of the show() method from my hide() method. Is this possible or am I overlooking something?

GoZoner

Since you are implementing the UICustomView, all you need to do is store the 'completion handler' as part of the UICustomView class. Then you call the handler when hide() is invoked.

class UICustomView : UIView {
   var onHide: ((Bool) -> ())?

   func show (onHide: (Bool) -> ()) {
     self.onHide = onHide
     let rootViewController: UIViewController = ...
     rootViewController.view.addSubview(self)
   }

   func hide (sender:UITapGestureRecognizer){
    if let customView = sender.view as? UICustomView{
        customView.removeFromSuperview()
        customView.onHide?(true)
    }
}

Of course, every UIView has a lifecycle: viewDidAppear, viewDidDisappear, etc. As your UICustomView is a subclass of UIView you could override one of the lifecycle methods:

class UICustomView : UIView {
  // ...

  override func viewDidDisappear(_ animated: Bool) {
     super.viewDidDisappear (animated)
     onHide?(true)
  }
}

You might consider this second approach if the view might disappear w/o a call to hide() but you still want onHide to run.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call Swift function with completion handler in objective c

From Dev

Call Swift function with completion handler in objective c

From Dev

Unable to make network call from completion handler?

From Dev

Unable to make network call from completion handler?

From Dev

Call a completion block from a delegate method

From Dev

Call a completion block from a delegate method

From Dev

Calling return of parent function from the completion handler

From Dev

How to call this completion handler?

From Dev

Is it possible to call another plugin's function from another plugin in OpenFlipper?

From Dev

Completion handler not called - iOS

From Dev

Completion handler not called - iOS

From Dev

NSURLSessionDataTask not executing the completion handler block

From Dev

how to call completion block?

From Dev

how to call completion block?

From Dev

Is it possible to call one function from another? (keeping separate files)

From Dev

Swift: Notifying a ViewController from another using completion handler

From Dev

Create a completion block that returns nothing but executes another function when completed?

From Dev

Evaluating javascript function from swift, getting nil in the completion handler

From Dev

how to call the handler from another application?

From Dev

ios 8 and Swift: call a function in another class from view controller

From Dev

invoke block from another function

From Dev

How to cancel a completion handler in iOS

From Dev

Is it possible to call a function as a parameter of another function in Swift?

From Dev

Completion Handler or function for this style of animation

From Dev

React - call handler from var in render function

From Dev

Bash completion from another completion

From Dev

UIAlertAction completion block not called - iOS

From Dev

iOS completion block not returning control

From Dev

Return values from completion handler

Related Related

  1. 1

    Call Swift function with completion handler in objective c

  2. 2

    Call Swift function with completion handler in objective c

  3. 3

    Unable to make network call from completion handler?

  4. 4

    Unable to make network call from completion handler?

  5. 5

    Call a completion block from a delegate method

  6. 6

    Call a completion block from a delegate method

  7. 7

    Calling return of parent function from the completion handler

  8. 8

    How to call this completion handler?

  9. 9

    Is it possible to call another plugin's function from another plugin in OpenFlipper?

  10. 10

    Completion handler not called - iOS

  11. 11

    Completion handler not called - iOS

  12. 12

    NSURLSessionDataTask not executing the completion handler block

  13. 13

    how to call completion block?

  14. 14

    how to call completion block?

  15. 15

    Is it possible to call one function from another? (keeping separate files)

  16. 16

    Swift: Notifying a ViewController from another using completion handler

  17. 17

    Create a completion block that returns nothing but executes another function when completed?

  18. 18

    Evaluating javascript function from swift, getting nil in the completion handler

  19. 19

    how to call the handler from another application?

  20. 20

    ios 8 and Swift: call a function in another class from view controller

  21. 21

    invoke block from another function

  22. 22

    How to cancel a completion handler in iOS

  23. 23

    Is it possible to call a function as a parameter of another function in Swift?

  24. 24

    Completion Handler or function for this style of animation

  25. 25

    React - call handler from var in render function

  26. 26

    Bash completion from another completion

  27. 27

    UIAlertAction completion block not called - iOS

  28. 28

    iOS completion block not returning control

  29. 29

    Return values from completion handler

HotTag

Archive