2015-07-04 124 views
5

我試圖通過自適應彈出窗口(例如,在iPad上的彈出窗口,iPhone上的全屏幕)以編程方式呈現視圖。爲了能夠解僱在iPhone上所呈現的視圖控制器,我想在一個導航控制器包裹它作爲https://stackoverflow.com/a/29956631/5061277或很好的例子在這裏:https://github.com/shinobicontrols/iOS8-day-by-day/tree/master/21-alerts-and-popovers/AppAlert,它看起來像:爲什麼不是presentationController:viewControllerForAdaptivePresentationStyle:被調用?

import UIKit 

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate { 

    @IBAction func handlePopoverPressed(sender: UIView) { 
    let popoverVC = storyboard?.instantiateViewControllerWithIdentifier("codePopover") as! UIViewController 
    popoverVC.modalPresentationStyle = .Popover 
    // Present it before configuring it 
    presentViewController(popoverVC, animated: true, completion: nil) 
    // Now the popoverPresentationController has been created 
    if let popoverController = popoverVC.popoverPresentationController { 
     popoverController.sourceView = sender 
     popoverController.sourceRect = sender.bounds 
     popoverController.permittedArrowDirections = .Any 
     popoverController.delegate = self 
    } 
    } 

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { 
    // This line _IS_ reached in the debugger 
    NSLog("Delagate asked for presentation style"); 
    return .FullScreen 
    } 

    func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? { 

    // This line _IS_NOT_ reached in the debugger 
    NSLog("Delegate asked for view controller"); 
    return UINavigationController(rootViewController: controller.presentedViewController) 


    } 
} 

雖然adaptivePresentationStyleForPresentationController調用委託方法時,不調用presentationController viewControllerForAdaptivePresentationStyle委託方法。因此,無法在iPhone上解除所呈現的控制器。

我已經在iOS 8.1到8.4上嘗試了XCode 6.4和7.0b2,都在模擬器和設備上,並且在任何情況下我的代表都不會要求viewControllerForAdaptivePresentationStyle。爲什麼?是否有我應該查看的構建設置,或者是否安裝了XCode 7以改變某些內容?這個確切的代碼顯示爲在上面的鏈接中工作。

回答

0

您需要添加這個方法:

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection{ 
    return UIModalPresentationOverFullScreen; 
} 
1

你需要將其配置後,目前它。或者使用segue使它更容易。

+0

儘管文檔暗示了其他情況,但popoverPresentationController必須在調用present之前配置委託,否則viewControllerForAdaptivePresentationStyle將永遠不會被調用。 – stevex

相關問題