Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modally present a ViewController, and then the run a callback function / block after the ViewController has been dismissed, without Delegate?

I was just knew that the completion block in the self.presentViewController(childVc, animated: true, completion: {}) is called after the child view controller is finish getting animated to be displayed at the screen. I actually want to run a block of code that get displayed after the animation of child view controller is finished getting animated to be dismissed. How can I do that, preferrably avoiding using delegate to do that?

EDIT: I call this from the presented (child) view controller like this: self.presentingViewController.dismissViewControllerAnimated (true) {}, but the problem is the child view controller can be presented from arbitrary page, and after the child is dismissed, the parent (presenting) view controller has to do different things. If I implement the call back in the child, I can't do different thing with different parent that call it.

For example, if I call login form as modal in the news screen, after the login form dismissed, I want to display comment section afterwards. But if the login form modally displayed from product screen, I want to display the user's cart with the product in the cart afterwards. If this is impossible to be done without delegate, I still interested in the delegate solution.

like image 845
Chen Li Yong Avatar asked Jun 01 '16 02:06

Chen Li Yong


2 Answers

You can add a property to your ChildVC to store a callback closure and set this when you present the child view controller. When the child view controller dismisses itself it can call the completion handler:

class ChildVC: UIViewController {        
    var completionHandler : ((childVC:ChildVC) -> Void)?

    func dismiss() {
        self.dismissViewControllerAnimated(true) {
            self.completionHandler?(childVC:self)
        }
    }
}

You supply the completion handler when you present the ChildVC instance:

let completionHandler:(ChildVC)->Void = { childVC in
    print("completed for \(childVC)")
}
childVC.completionHandler=completionHandler
self.presentViewController(childVC, animated: true, completion: nil)
like image 80
Paulw11 Avatar answered Nov 20 '22 07:11

Paulw11


Use dismissViewControllerAnimated API which takes a completion block.

    self.dismissViewControllerAnimated(true) { 
        /* Do callback stuff here*/
    }
like image 1
Sonny Saluja Avatar answered Nov 20 '22 07:11

Sonny Saluja