popoverPresentationController is nil for UIAlertController on iPhone

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

Replies

I just tripped on your post. I've the same problem, and nothing I can find on StackOverflow helps at all. Just to be clear - you did run this when the device was in landscape, right?


What is really maddening is that the HIG explicitly tells you that in a Regular horizonal environment, to show the ActionSheet as a popup. Yet nothing I do (and I read about a dozen posts on this on SO) does anything.


Time for Bug Reporter...rdar://23361006

Hi,

I am experiencing the exact same problem.

Have you found a solution to the problem??


Thanks,

Alex

Apple just closed my rdar - "Engineering has determined that this issue behaves as intended."


So the feature doesn't work the way the HIG says it should, but neither the HIG nor the code are wrong.


Sigh - this isn't the Apple I learned to love oh so long ago :-(