Correct way of showing consecutive modalViews

前端 未结 7 1858
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 23:44

I have two views that need to be shown modally, one after the other. This doesn\'t work if we dismiss and show consecutively, like this:

[rootController dism         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 00:22

    I find using the modal view's -viewDidDissapear to invoke methods on the presenting view controller work's very well. One benefit is the ability to delay deallocation on the modal view controller. Please post any improvements I can make. My inspiration for creating this protocol came from iOS 5's "dismissViewControllerAnimated:completion:" addition to UIViewController. I wanted this functionality in iOS 4.3.


    PresentorDelegateProtocol.h

    @protocol PresentorDelegateProtocol 
    @optional
    
    /* 
    
    Extra protocol methods defined in protocol for flexibility.  
    Main methods are:
    - (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated; 
    - (void)modalViewDissapeared:(id)modalView;  //used in modal view's -viewDidDissapear
    
    */
    
    - (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated;
    - (void)modalViewDissapeared:(id)modalView; 
    
    // use the block in this method send messages to save state, etc.  This is the one I like to use.
    - (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block;
    
    // use in other classes that are not controlling dismissal of the modal view
    - (void)executeBlockOnModalDissapearance: (void(^)())block;
    
    @end
    

    PresentingViewController.h

    #import "PresentorDelegateProtocol.h"
    @interface PresentingViewController : UIViewController 
    - (void)showModalVC;
    @end
    

    ModalViewController.h

    #import "PresentorDelegateProtocol.h"
    @interface ModalViewController : UIViewController
    @property (nonatomic, assign) id  presentorDelegate;
    - (void)close;
    @end
    

    PresentingViewController.m

    #import "PresentingViewController.h"
    #import "ModalViewController.h"
    @implementation PresentingModalViewController
    - (void)showModalVC
    {
        ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];
        modalVC.presentorDelegate = self;
        [self presentModalViewController:modalVC animated:YES];
    }
    - (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated
    {
        if ([modalView isKindOfClass:[ModalViewController class]]) {
            NSLog(@"Can invoke based on class"); 
        }
        [self dismissModalViewControllerAnimated:animated];    
    }
    - (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block
    {
        block();  
        /* execute block before or after calling to dismiss modal view */
        [self dismissPresentingModalViewController:modalView animated:animated];
        //block();
    }
    - (void)modalViewDissapeared:(id)modalView
    {
        if ([modalView isKindOfClass:[ModalViewController class]]) {
            NSLog(@"Do stuff based on class.");
        }
    }
    - (void)executeBlockOnModalDissapearance: (void(^)())block
    {
        block();
        NSLog(@"This delay's dealloc on modal view until block completes");
    }
    @end
    

    ModalViewController.m

    #import "ModalViewController.h"
    @implementation ModalViewController
    @synthesize presentorDelegate;
    
    - (void)close
    {
        if (1 == 0 /*need to do something before dealloc*/){
            [self.presentorDelegate dismissPresentingModalViewController:self animated:YES withBlock:^{
                NSLog(@"Do stuff with block.  Save, animate, etc");
            }];
    
        } else {
            [self.presentorDelegate dismissPresentingModalViewController:self animated:YES];
        }
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        if (1 == 0 /*stuff to do*/){
            [self.presentorDelegate executeBlockOnModalDissapearance:^{
            // do stuff before modal view is deallocated
            }];
        }
        [self.presentorDelegate modalViewDissapeared:self];
    
        presentorDelegate = nil;
        [super viewDidDisappear:animated];
    }
    @end;
    

提交回复
热议问题