简体   繁体   中英

Dismiss two present ModalViewControllers (MPMediaPickerController and UIViewController) at the same time

I have a strange problem while dismissing two modalviewcontrollers.

What I am doing is I have a viewControllers A and B. I am presentingmodalviewController B on A. And then On BI am presenting MPMediaPickerController on B. Now my issue is After clicking on Done button in MPMediaPickerController its delegate method is calling . I have implemented the below code to dismiss the MPMediaPickerController and Controller B,so that We can go to Controller A directly.

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{  
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES];
}

Same code is working for ViewControllers A, B and C but not working if third one is MPMediaPickerController

Any ideas or suggestions are very helpful.

Why are you using dismissModalViewControllerAnimated method ? It is deprecated, use dismissViewControllerAnimated instead.

Check with this code:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{ 
   [self dismissViewControllerAnimated:YES completion:^{
        [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    }];
}

Whenever you present MPMediaPickerController from a controller then you have to set its delegate to the presenting controller. So as because delegate method is inside the presenting view controller, you have to call dismissModalViewControllerAnimated instead of whatever you are doing. I am just passing bool parameter to NO because whenever you will try to dismiss more then one viewcontroller simultaneously at that time there will be unbalanced transition call by the iOS and that may prevent another call. So i just dismissed MPMediaPickerController without any animation and the presenting view controller with animation.

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{  
    [self dismissModalViewControllerAnimated:NO];
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}

have you tried the below codes?

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{  
    [mediaPicker dismissModalViewControllerAnimated:NO];

    [self dismissModalViewControllerAnimated:YES]; 

}

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM