Custom transition animation not calling VC lifecycle methods on dismiss

后端 未结 6 1530
梦谈多话
梦谈多话 2020-12-24 11:11

So I built a custom presenting transition animation and everything seems to be working great except the view controller lifecycle methods are not being called on dismiss.

6条回答
  •  孤城傲影
    2020-12-24 11:56

    Ah, this is a modal presentation. I don't believe viewWillAppear and viewDidAppear are called with custom transition using the method, as the view is technically still active in the view hierarchy. I'd suggest using delegation here as you normally would with a presented modal.

    Create delegate protocol on the presented VC. Create a delegate method that can be called from the presenting VC. As you present the overlay, set the presenting VC as the delegate. Then, call that delegate method from the presented VC. Then you can call any sort of actions from within the presenting VC before you call dismissViewController

    In your overlay (ModalViewController.h):

    @protocol ModalViewDelegate 
    -(void)didDismissModalView;
    @end
    
    @interface ModalViewController : UIViewController
    @property(strong, nonatomic) id  delegate;
    

    In your ModalViewController.m, call a method that calls your delegate method:

    - (void)dismissModal{
        [self.delegate didDismissModalView];
    }
    

    In your presenting VC h file: (PresentingViewController.h), make this class conform to your modal delegate protocol:

    @interface PresentingViewController : UIViewController 
    

    In your presenting VC, as you present the modal:

    ...
    ModalViewController *modalViewController = [[ModalViewController alloc] init];
    modalViewController.delegate = self; //the makes the presenting VC the delegate
    [self presentViewController:modalViewController animated:YES completion:nil];
    ...
    

    Finally, in your presenting VC, when you want to perform some actions before dismissing the modal, implement the ModalViewDelegate method:

    - (void)didDismissModalView{
        //DO SOME COOL STUFF, SET UP STUFF HERE, UPDATE UI, ETC
    
        //Then dismiss the modal
       [self dismissViewControllerAnimated:YES completion:^{
          //Do more cool stuff
        }];
    }
    

    All of this will work with your current custom transition code, but will give you more control over what happens before the modal/overlay is dismissed. Delegate is a beautiful thing.

    Further, this will keep this animation code separate from code for other functionality, which is a bit cleaner IMO.

提交回复
热议问题