13

I assign popover object o it's contentViewController and I put dismiss code in a button which resides in the content view controller.

When the button is pressed:

 [self.popover dismissPopoverAnimated:YES];

is called and popover is dismissed. However, delegate's method is not called automatically. I thought that I was not setting it's delegate, but it is there. If I add the following line after dismissPopoverAnimated line, delegate is called correctly...

 [self.popover.delegate popoverControllerDidDismissPopover:self.popover];

I don't understand why it doesn't "automatically" call the delegate's method. How can this happen?

frankish
  • 6,738
  • 9
  • 49
  • 100
  • I assume you're talking about delegates of protocols? That's how it works. When you want a method defined in the protocol to execute for your delegate, you need to explicitly tell your delegate to do so using [delegate protocolMethod]; at the location of where you want the method to execute. Imagine if you had 10 method in your protocol. In that scenario, you wouldn't want your delegate to execute all 10 methods right? So it can't just guess which method you want to call, even if there's only one method defined. – Zhang Jan 12 '14 at 15:20
  • Normally, using dismissPopoverAnimated method automatically calls popoverControllerDidDismissPopover method itself after the popover is dismissed. That's the way UIPopoverController lets us now about the dismissal. – frankish Jan 12 '14 at 16:32
  • possible duplicate of [Dismissing UIPopoverController with -dismissPopoverAnimated: won't call delegate?](http://stackoverflow.com/questions/3567033/dismissing-uipopovercontroller-with-dismisspopoveranimated-wont-call-delegate) – Keith Smiley May 30 '14 at 23:24

3 Answers3

16

The popoverControllerDidDismissPopover: in the delegate is not called when 'dismissPopoverAnimated:' is used.

From the Apple Documentation for popoverControllerDidDismissPopover: in UIPopoverControllerDelegate:

The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated: method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.

RKHessel
  • 176
  • 1
  • 3
4

There are two ways to dismiss a popover. (a) tapping outside the popover; and (b) doing it programmatically with

[self.popover dismissPopoverAnimated:YES];

If you do it programmatically, then the docs (https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverControllerDidDismissPopover:) say:

The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated: method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.

Thus, not calling the delegate automatically is the normal behavior, and what you're doing (calling it yourself) is fine.

auspicious99
  • 3,902
  • 1
  • 44
  • 58
1

popoverControllerDidDismissPopover is not called on Dismiss, but its called when you click outside the popoverController contentview.

https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html#jumpTo_4

Edwin O.
  • 4,998
  • 41
  • 44