Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a presented view controller is dismissed

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.

like image 567
user523234 Avatar asked Sep 29 '15 20:09

user523234


People also ask

How do you dismiss the presenting view controller?

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.

What happens when you dismiss a view controller?

The block to execute after the view controller is dismissed. This block has no return value and takes no parameters.

What is UIViewController in IOS?

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.

What is the life cycle of view controller?

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.


1 Answers

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.     } } 
like image 74
Joris Weimar Avatar answered Sep 22 '22 15:09

Joris Weimar