Hi,
I'm experiencing a problem in which a UIAlertController's popoverPresentationController reference is always nil. The problem appears to exist only on iPhone. The example code below functions as expected on iPad.
The intent of the code is to display an action sheet as a popover on iPad and iPhone. For iPhone, the UIPopoverPresentationControllerDelegate method adaptivePresentationStyleForPresentationController is used to return UIModalPresentationStyle.None.
Example:
class MyViewController: UIViewController, UIPopoverPresentationControllerDelegate {
// Declare alert controller
var ac: UIAlertController!
...
// Create the alert controller
func createAlertController() {
ac = UIAlertController(title: "Title", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
ac.modalPresentationStyle = .Popover
// add actions...
}
func alertButtonOnPress(sender: AnyObject) {
presentViewController(ac, animated: true, completion: nil)
// popoverPresentationController never sets for iPhone
ac.popoverPresentationController?.delegate = self
if nil != ac.popoverPresentationController {
let presentationController: UIPopoverPresentationController = ac.popoverPresentationController!
presentationController.sourceRect = aRect
presentationController.sourceView = self.view
}
}
// This delegate method is not called on iPhone because it doesn't get set in alertButtonOnPress() above
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
...
}
FWIW, the code works when popover content is other than a UIAlertController (e.g. a plain UIViewController).
Is there a better way to set up the presentationController? Is there a known issue setting popoverPresentationController on a UIAlertController in iPhone? Any suggestions welcome.
Thanks!
Jesse