Is the method called when the UIViewController is rejected?

Is there a general best practice way to get notified when the current view manager rejects (either popped out or removed ModalDialog'd)? I cannot use -viewWillDisappear :, since this is also called when another viewController is clicked on top of the current one.

+5
source share
5 answers
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self addObserver:self forKeyPath:@"parentViewController" options:0 context:NULL];
    }
    return self;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([@"parentViewController" isEqualToString:keyPath] && object == self) {
        if (!self.parentViewController)
            NSLog(@"Dismissed");
    }
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"parentViewController"];
    [super dealloc];
}
+11
source

, , UIViewController modalViewController, "didDismiss..." .

0

?

, :

ViewcontrollerONE ViewControllerTWO . ViewControllerTWO . ViewControllerONE , ViewControllerTWO XYZ- - .

, :

VC1 VC2. VC2 VC1 .

0

KVO iOS 8.

UIViewController , dismissAnimated:completion: dismissViewControllerAnimated:completion:. .

#define DismissNotifyViewControllerDismissedNotification  @"DismissNotifyViewControllerDismissed"


@interface DismissNotifyViewController : UIViewController

- (void)dismissAnimated:(BOOL)flag completion:(void (^)(void))completion;

@end


@implementation DismissNotifyViewController

- (void)dismissAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [self.presentingViewController dismissViewControllerAnimated: flag
                                                      completion: ^{

          if (completion)
               completion();

          [NSNotificationCenter.defaultCenter 
                     postNotificationName: DismissNotifyViewControllerDismissedNotification
                     object: self];
    }];
}

@end
0

Apple iOS8, presentationControllers, presentationControllers KVO, containerView, removedFromSuperview , -[UIPresentationController transitionDidFinish:]. iOS8 :

self.presentationContext.presentViewController(self.viewControllerToPresent, animated: true, completion: { _ in
     self.viewControllerToPresent.presentationController?.addObserver(self, forKeyPath: "containerView", options: [], context: &self.observingContext)
})

, Handler, , viewController.

In the observer value, I have to remove the observation when the container view no longer exists:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    guard &self.observingContext == context else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        return
    }
    if let presentationController = object as? UIPresentationController where presentationController.containerView == nil {
        presentationController.removeObserver(self, forKeyPath: "containerView")
    }
}
0
source

All Articles