Ios – Dismissing a UIPopoverController from within its contentViewController

cocoa-touchiosiphoneuipopovercontroller

If you want to dismiss a popover — for example, from a button within the popover's contentViewController you must —

  1. Create a reference to the popover to be held by view controller which creates it
  2. Create a notification from the contentViewController to let the owning view controller know that it should be dismissed, or alternately create a delegate for the same purpose
  3. Send the notification or delegate message when the popover is ready to be dismissed
  4. Call dismissPopover:animated when the notification or delegate method is called

Meanwhile, from a UIViewController you can access the modal view controller, the parent view controller, the navigation controller, the split view controller, the tab bar controller, the search display controller, the child view controllers, the presenting view controllers, and the presented view controllers.

Is there a better approach to do this from popover's contentViewController?

Best Answer

Unfortunately, you'll have to create a weak property reference to said UIPopoverController as there's no way to access it from within the content view controller.

I was surprised how UIViewControllers can access the modal view controller, the parent view controller, the navigation controller, the split view controller, the tab bar controller, the search display controller, and as of iOS 5, the child view controllers as well as presenting and presented controllers...but not the popover controller (granted popovers aren't UIViewControllers but still).

Technically, there's a private, undocumented method to retrieve the popoverController that the UIViewController is in...I have no idea why they never made it public given that it should be exactly the same as any of the above controllers.

Though even in the private, undocumented world, there's no equivalent to dismissModalViewcontrollerAnimated:. You'll still have to get that reference then dismiss it that way.

Related Topic