2015-07-04 12 views
5

Próbuję programowo przedstawić widok poprzez adaptacyjny popover (np. Popover na iPadzie, pełny ekran na iPhonie). Aby móc odrzucić przedstawione kontroler widok na iPhone, próbowałem owijając go w kontroler nawigacji jak w https://stackoverflow.com/a/29956631/5061277 lub dobry przykład tutaj: https://github.com/shinobicontrols/iOS8-day-by-day/tree/master/21-alerts-and-popovers/AppAlert, który wygląda następująco:Dlaczego nie jest presentationController: viewControllerForAdaptivePresentationStyle: jest wywoływana?

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) 


    } 
} 

Choć adaptivePresentationStyleForPresentationController Metoda delegate jest wywoływana, nie jest wywoływana metoda presentationController viewControllerForAdaptivePresentationStyle delegate. W rezultacie nie ma możliwości odrzucenia przedstawionego kontrolera na iPhonie.

Próbowałem XCode 6.4 i 7.0b2 na iOS 8.1 do 8.4, zarówno w symulatorze i na urządzeniu, i w żadnym przypadku mój uczestnik nie poprosił o viewControllerForAdaptivePresentationStyle. Czemu? Czy istnieje ustawienie kompilacji, na które powinienem patrzeć, czy też może XCode 7 musi coś zmienić? Ten dokładny kod jest prezentowany jako działający w linkach powyżej.

Odpowiedz

0

Trzeba dodać tę metodę:

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

Musisz przedstawić go po skonfigurowaniu go. Możesz też użyć segue, aby było to łatwiejsze.

+0

Mimo że dokumentacja sugeruje inaczej, popoverPresentationController musi mieć skonfigurowany delegat przed wywołaniem obecnej, lub viewControllerForAdaptivePresentationStyle nigdy nie zostanie wywołana. – stevex

Powiązane problemy