When a modalView is presented, a network event generates a new modal view controller. what I'm doing is to chain the presentViewController:animated inside of dismissViewControllerAnimated:completion like this:

//    ModalViewController *vc = ...
    if (self.presentedViewController) {
        __weak MyViewController *me = self;
        [self.presentedViewController dismissViewControllerAnimated:YES
                                                         completion:
         ^{
             // need a delay to call?
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [me presentViewController:vc
                                  animated:YES
                                completion:nil];
             });
         }];
    }else{        
        [self presentViewController:vc animated:YES completion:nil];
    }

It all goes well: the original modal viewcontroller dismissed, the network generated one presented, and user can dismiss it successfully. however, when trying to present a 3rd modalViewController, it failed with error:

2014-03-26 15:49:52.111 coshop[6046:60b] Warning: Attempt to dismiss from view controller <RootViewController: 0xa8b54a0> while a presentation or dismiss is in progress!

I also tried this:

if (self.presentedViewController) {
    __weak MyViewController *me = self;
    [self.presentedViewController dismissViewControllerAnimated:YES
                                                     completion:nil];

         // dismiss animation ends within 0.5.
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [me presentViewController:vc
                              animated:YES
                            completion:nil];
         });

}else{        
    [self presentViewController:vc animated:YES completion:nil];
}

Any suggestion? thanks!

有帮助吗?

解决方案

Just because the VC doesn't appear anymore in the simulator or your device, doesn't mean it's animation hasn't fully completed.

I suspect that your "3rd modalViewController" is not being presented after the second network generated view controller is dismissed. The reason it works in the first case is because you have it presenting in the dismissed vc's dismiss completion block.

If the 3rd is presented by a user action, you need to ensure that they can't perform that action before the other vc's dismissal or presentation has completed.

What you might want to do is set the user's action up in a block that you then pass as a parameter to the class that presents the next view controller, and execute the block from the presentation completion block.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top