Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a drop-in category on <code>UIPopoverController</code> that does what you're asking.</p> <p>Basically the category swizzles <code>initWithContentViewController:</code> so that it can track live <code>UIPopoverController</code> instances in a <code>NSHashTable</code> (which doesn't itself hold the contained UIPopoverControllers alive since it keeps weak references to them.) It also monitors for <code>UIApplicationDidEnterBackgroundNotification</code>, and when this arrives it iterates the collection of live UIPopoverControllers and dismisses any that are showing.</p> <p>It might be nice to extend this to implement the "never allow two popovers to show at once" rule that Apple has.</p> <p>I'm not a huge fan of method swizzling in production apps but this seems pretty safe.</p> <p>No special instructions for use. Just include the category in your project and use your UIPopoverControllers normally.</p> <pre><code>#import &lt;objc/runtime.h&gt; @interface UIPopoverController (autodismiss) @end @implementation UIPopoverController (autodismiss) static NSHashTable* ts_popoverHashTable; + (void) load { SEL originalSelector = @selector(initWithContentViewController:); SEL replacementSelector = @selector(ts_initWithContentViewController:); Method originalMethod = class_getInstanceMethod( [UIPopoverController class], originalSelector); Method replacementMethod = class_getInstanceMethod( [UIPopoverController class], replacementSelector); method_exchangeImplementations(originalMethod, replacementMethod); [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector( applicationDidEnterBackgroundNotification: ) name: UIApplicationDidEnterBackgroundNotification object: nil]; } - (id) ts_initWithContentViewController: (UIViewController*) contentViewController { UIPopoverController* pc = [self ts_initWithContentViewController: contentViewController]; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ ts_popoverHashTable = [NSHashTable weakObjectsHashTable]; }); [ts_popoverHashTable addObject: pc]; return pc; } + (void) applicationDidEnterBackgroundNotification: (NSNotification*) n { for ( UIPopoverController* pc in ts_popoverHashTable ) { if ( pc.isPopoverVisible ) { [pc dismissPopoverAnimated: NO]; } } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload