Problem with Modal View Controllers and defines PresentationContext

I created a custom container view controller using the new UIViewController container container controller methods in iOS 5.

The problem is that my child container controller UIViewController has definesPresentationContext = YES , when it creates and is another modal view controller , UIKit sets the container (and not the child) as the representing controller.

For example, in MyChildViewController.m:

 - (void)showMailComposeView:(id)sender { __block MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init]; vc.mailComposeDelegate = self; vc.subject = @"Subject"; self.definesPresentationContext = YES; [self presentViewController:vc animated:YES completion:^{ if ([self.modalViewController isEqual:vc]) NSLog(@"This should print..."); if ([vc.presentingViewController isEqual:self.parentViewController]) NSLog(@"... but this shouldn't"); // NOTE: Both log statements printed }]; } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self dismissViewControllerAnimated:YES completion:^{}]; // NOTE: self.parentViewController.view now displays instead of self.view } 

Where am I going wrong?

How can I provide a view for the child that opens when the modal view is rejected (and not the container view)?

+6
source share
1 answer

Add this line before the view controller view:

 vc.modalPresentationStyle = UIModalPresentationCurrentContext 

If you performed all the correct actions of the parent child up to the view controller chain, this forces the view to replace the view MyChildViewController, and then the view MyChildViewController returns when the view is rejected.

Oh, and I forgot to mention, even then it will only work on the iPad. The presented view controller view always occupies the entire screen on the iPhone - it is always displayed in the root view.

EDIT: Starting with iOS 8, this feature is also available on the iPhone. (As well as popovers and split views - basically, most iPad-only form statements have become false with iOS 8, which in my opinion is terrific news.)

+17
source

Source: https://habr.com/ru/post/900255/


All Articles