2011-10-28 74 views
6

我創建使用iOS中5問題與模態視圖控制器和definesPresentationContext

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 
} 

我要去哪裏錯了?

我如何確保它是孩子視圖,當模態視圖被取消(而不是容器視圖)時顯示?

回答

17

呈現視圖控制器之前添加此行:

vc.modalPresentationStyle = UIModalPresentationCurrentContext 

如果你已經做了所有正確的親子東西一路視圖控制器鏈,這將導致所提出的觀點,以取代MyChildViewController的視圖,然後MyViewViewController的視圖將在所呈現的視圖解除時返回。

哦,我忘了提及,即使這樣,它也只能在iPad上使用。呈現的視圖控制器的視圖總是佔用iPhone上的整個屏幕 - 它總是從根視圖呈現。

編輯:從iOS 8開始,此功能也可在iPhone上使用。 (彈出窗口和分割視圖也是如此 - 基本上,「僅在iPad上」的表單的大多數聲明在iOS 8中變得虛假,我認爲這是真棒新聞。)

+0

雖然我的問題有點不同,但這個爲我工作!謝謝! – chadbag

+0

同樣在這裏。這次真是萬分感謝! – Robert

+2

有趣的是,爲什麼在iPhone上,模態總是從根視角出發。我在iPhone上有類似的容器情況,並做了以上所有的工作,我想知道爲什麼模態視圖沒有從子視圖控制器的視圖範圍內呈現出來。聽起來這只是一種與iPad不同的iPhone行爲。 – idStar