我使用iOS 5中的new UIViewController container view controller methods创建了一个自定义容器视图控制器。
麻烦的是,即使我的容器控制器的子UIViewController有definesPresentationContext = YES
,当它creates and presents another modal view controller时,UIKit将容器(而不是子容器)设置为呈现控制器。
例如,在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
}
我哪里错了?
如何确保在模态视图被解除时(而不是容器视图)显示 child 视图?
答案 0 :(得分:18)
在呈现视图控制器之前添加此行:
vc.modalPresentationStyle = UIModalPresentationCurrentContext
如果你已经在视图控制器链中完成了所有正确的父子事物,这将导致呈现的视图替换MyChildViewController的视图,然后当呈现的视图被解除时,MyChildViewController的视图将返回。
哦,我忘了提,即使这样,这只能在iPad上使用。呈现的视图控制器视图始终占据iPhone的整个屏幕 - 它始终从根视图中显示。
编辑:从iOS 8开始,此功能也可在iPhone上使用。 (弹出窗口和拆分视图也是如此 - 基本上,大多数“仅在iPad上”的形式的陈述在iOS 8中变得错误,在我看来这是非常棒的消息。)