Let's say, I have an instance of a view controller class called VC2. In VC2, there is a "cancel" button that will dismiss itself. But I can't detect or receive any callback when the "cancel" button got trigger. VC2 is a black box.
A view controller (called VC1) will present VC2 using presentViewController:animated:completion:
method.
What options does VC1 have to detect when VC2 was dismissed?
Edit: From the comment of @rory mckinnel and answer of @NicolasMiari, I tried the following:
In VC2:
-(void)cancelButton:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ }]; // [super dismissViewControllerAnimated:YES completion:^{ // // }]; }
In VC1:
//-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^ _Nullable)(void))completion { NSLog(@"%s ", __PRETTY_FUNCTION__); [super dismissViewControllerAnimated:flag completion:completion]; // [self dismissViewControllerAnimated:YES completion:^{ // // }]; }
But the dismissViewControllerAnimated
in the VC1 was not getting called.
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.
The block to execute after the view controller is dismissed. This block has no return value and takes no parameters.
The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.
The view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.
There is a special Boolean property inside UIViewController
called isBeingDismissed
that you can use for this purpose:
override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) if isBeingDismissed { // TODO: Do your stuff here. } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With