2015-09-28 79 views
37

我使用這個代碼代碼如何解決UIPopoverController不推薦使用的警告消息?

mediaLibraryPopover = [[UIPopoverController alloc] 
         initWithContentViewController:avc]; 
[self.mediaLibraryPopover presentPopoverFromRect:[theButton bounds] 
          inView:theButton 
          permittedArrowDirections:UIPopoverArrowDirectionAny 
          animated:YES]; 

而且我得到在Xcode-7這樣的警告。

UIPopoverController is deprecated ,first deprecated in iOS 9.0 - UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIM 
+3

如果我知道如何刪除警告,那麼我沒有問過問這個問題。而且當你也不知道如何刪除警告時,你評論道。 –

+0

UIStorybaordPopoverSegue.h中的棄用警告已得到改進,現在提供了方向:「從您的segue的performHandler中訪問destinationViewController.popoverPresentationController ...」 – Mark

回答

91

您不再需要UIPopoverController來呈現視圖控制器。 相反,您可以將視圖控制器的modalPresentationStyle設置爲UIModalPresentationPopover

您可以使用下面的代碼爲:

avc.modalPresentationStyle = UIModalPresentationPopover; 
avc.popoverPresentationController.sourceView = theButton; 
[self presentViewController:avc animated:YES completion:nil]; 

UIModalPresentationPopover

在一個水平定期的環境,其中內容顯示在酥料餅視圖 的演講風格。 背景內容變暗,並在彈出窗口外點擊,導致彈出窗口被取消。如果您不想點擊以關閉彈出窗口,則可以將一個或多個視圖分配給相關UIPopoverPresentationController對象的透視視圖 屬性,您可以從popoverPresentationController屬性中獲取該屬性,其中包含 。

在水平緊湊環境中,此選項的行爲與 UIModalPresentationFullScreen相同。

適用於iOS 8.0或更高版本。

參考UIModalPresentationStyle Reference


您需要設置任何sourceViewbarButtonItem財產,否則它會與下面的消息崩潰:

***終止應用程序由於未捕獲的異常' NSGenericException',原因:'UIPopoverPresentationController(***)在演示發生之前應該有一個非零 sourceView或barButtonItem。

要正確定位彈出箭頭,還需要指定sourceRect屬性。

avc.modalPresentationStyle     = UIModalPresentationPopover; 
avc.popoverPresentationController.sourceView = self.view; 
avc.popoverPresentationController.sourceRect = theButton.frame; 
[self presentViewController:avc animated:YES completion:nil]; 

參考sourceViewsourceRect的更多細節。

+4

我發現我需要更改'avc.popoverPresentationController.sourceView = self.view; '並且添加'avc.popoverPresentationController.sourceRect = theButton.frame;'讓彈窗被正確定位。 – mark

+0

@mark有一個好點,但我不認爲你需要改變sourceView。當我這樣做時,我離開了'avc.popoverPresentationController.sourceView = theButton;'原樣並簡單地添加了'avc.popoverPresentationController.sourceRect = theButton。' – Alex311

+1

@downvoter:你能告訴我爲什麼你投了票,這樣我就可以改進我的答案。 –

-5

編輯:關於反對票。首先一個有趣和相關的故事:

http://www.folklore.org/StoryView.py?story=Negative_2000_Lines_Of_Code.txt

我下面貼,以幫助程序員可能一直停留在心態,呈現媒體選擇器UI時的iPad/iPhone仍然需要獨立的執行路徑的答案。在我原來的帖子時,這在其他答案中還不清楚。此解決方案路徑簡化了我的代碼以及我的應用程序的未來維護。我認爲其他人可能會認爲這種觀點很有用,因爲有時刪除正確的代碼行可能是一個很好的解決方案。

編輯結束。

如果你已經有一個工作程序,只是想擺脫折舊的警告這可能適合你。

在我的代碼和我的理解中,Popovers是爲iPad引入的,並且是iPad專用的。蘋果似乎改變了這一點。所以,如果你已經有使用這樣的一個工作程序:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    // use popovers 
else 
    // don't use popovers 

你可以做的是剛剛擺脫了iPad專用代碼(這可能是使用Popovers的代碼),並讓你的程序運行iPhone和iPad的相同說明。這對我有效。

+5

答案沒有用:不言而喻,刪除導致不需要警告的代碼將刪除警告。 – ToolmakerSteve

+0

但是,你不再需要特殊的代碼來處理iPad上的圖像選擇器,這就是我試圖做的。 –

+0

此外,您的答案意味着刪除代碼仍然會在iPad上提供彈出功能,但情況並非如此。它只是完全放棄了popover支持,這是一個重要的iPad用戶界面功能。 – Markus

1

如果直接從按鍵操作則可以使用此代碼

SecondViewController *destinationViewController = (SecondViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"second"]; 
destinationViewController.modalPresentationStyle = UIModalPresentationPopover; 
destinationViewController.popoverPresentationController.sourceView = self.customButton; 

// Set the correct sourceRect given the sender's bounds 
destinationViewController.popoverPresentationController.sourceRect = ((UIView *)sender).bounds; 
[self presentViewController:destinationViewController animated:YES completion:nil]; 
4

蘋果已經提出和iOS8上這裏配置popovers官方途徑希望:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html

雖然類似@ MidhunMP的答案,這是值得一提的段落:

配置酥料餅的演示控制器調用 presentViewController:animated:completion:可能會看到後, m 違反直覺,但直到您啓動演示文稿之後,UIKit纔會創建演示文稿控制器 。此外,UIKit必須等待 ,直到下一個更新週期才能在屏幕上顯示新內容。 延遲時間讓您有時間爲您的彈出窗口配置演示控制器 。

如果您想要(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationControllerDelegate_protocol/index.html),配置和響應事件也可以通過委託完成。

一個例子,撇開使用委託的:

// Present the controller using the popover style. 
controller.modalPresentationStyle = UIModalPresentationPopover; 
[self presentViewController:controller animated:YES completion:nil]; 

// Popover presentation controller was created when presenting; now configure it. 
UIPopoverPresentationController *presentationController = 
     [controller popoverPresentationController]; 
presentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft; 
presentationController.sourceView = containerFrameOfReferenceView; 
// arrow points out of the rect specified here 
presentationController.sourceRect = childOfContainerView.frame; 

但你也想放棄。要做到這一點,而無需使用委託,您呈現控制器可以叫:

[self dismissViewControllerAnimated:YES completion:nil]; 

但如果我轉動我的設備,並且酥料餅不指向正確的區域?您的提供控制器可以處理它:

// Respond to rotations or split screen changes 
- (void)viewWillTransitionToSize:(CGSize)size 
     withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; 
    [coordinator animateAlongsideTransition:nil 
           completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { 
     // Fix up popover placement if necessary, after the transition. 
     if (self.presentedViewController) { 
      UIPopoverPresentationController *presentationController = 
        [self.presentedViewController popoverPresentationController]; 
      presentationController.sourceView = containerFrameOfReferenceView; 
      presentationController.sourceRect = childOfContainerView.frame; 
     } 
    }]; 
} 
相關問題