How to Dismiss a Storyboard Popover How to Dismiss a Storyboard Popover ios ios

How to Dismiss a Storyboard Popover


EDIT: These problems appear to be fixed as of iOS 7.1 / Xcode 5.1.1. (Possibly earlier, as I haven't been able to test all versions. Definitely after iOS 7.0, since I tested that one.) When you create a popover segue from a UIBarButtonItem, the segue makes sure that tapping the popover again hides the popover rather than showing a duplicate. It works right for the new UIPresentationController-based popover segues that Xcode 6 creates for iOS 8, too.

Since my solution may be of historical interest to those still supporting earlier iOS versions, I've left it below.


If you store a reference to the segue's popover controller, dismissing it before setting it to a new value on repeat invocations of prepareForSegue:sender:, all you avoid is the problem of getting multiple stacking popovers on repeated presses of the button -- you still can't use the button to dismiss the popover as the HIG recommends (and as seen in Apple's apps, etc.)

You can take advantage of ARC zeroing weak references for a simple solution, though:

1: Segue from the button

As of iOS 5, you couldn't make this work with a segue from a UIBarButtonItem, but you can on iOS 6 and later. (On iOS 5, you'd have to segue from the view controller itself, then have the button's action call performSegueWithIdentifier: after checking for the popover.)

2: Use a reference to the popover in -shouldPerformSegue...

@interface ViewController@property (weak) UIPopoverController *myPopover;@end@implementation ViewController- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // if you have multiple segues, check segue.identifier    self.myPopover = [(UIStoryboardPopoverSegue *)segue popoverController];}- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {    if (self.myPopover) {        [self.myPopover dismissPopoverAnimated:YES];        return NO;    } else {        return YES;    }}@end

3: There's no step three!

The nice thing about using a zeroing weak reference here is that once the popover controller is dismissed -- whether programmatically in shouldPerformSegueWithIdentifier:, or automatically by the user tapping somewhere else outside the popover -- the ivar goes to nil again, so we're back to our initial state.

Without zeroing weak references, we'd have to also:

  • set myPopover = nil when dismissing it in shouldPerformSegueWithIdentifier:, and
  • set ourself as the popover controller's delegate in order to catch popoverControllerDidDismissPopover: and also set myPopover = nil there (so we catch when the popover is automatically dismissed).


I found the solution here https://stackoverflow.com/a/7938513/665396In first prepareForSegue:sender: store in a ivar/property the pointer to the UIPopoverController and user that pointer to dismiss the popover in the subsequent invocations.

...@property (nonatomic, weak) UIPopoverController* storePopover;...- (void)prepareForSegue:(UIStoryboardSegue *)segue                  sender:(id)sender {if ([segue.identifier isEqualToString:@"My segue"]) {// setup segue here[self.storePopover dismissPopoverAnimated:YES];self.storePopover = ((UIStoryboardPopoverSegue*)segue).popoverController;...}


I've used custom segue for this.

1

create custom segue to use in Storyboard:

@implementation CustomPopoverSegue-(void)perform{    // "onwer" of popover - it needs to use "strong" reference to retain UIPopoverReference    ToolbarSearchViewController *source = self.sourceViewController;    UIViewController *destination = self.destinationViewController;    // create UIPopoverController    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:destination];    // source is delegate and owner of popover    popoverController.delegate = source;    popoverController.passthroughViews = [NSArray arrayWithObject:source.searchBar];    source.recentSearchesPopoverController = popoverController;    // present popover    [popoverController presentPopoverFromRect:source.searchBar.bounds                                        inView:source.searchBar                     permittedArrowDirections:UIPopoverArrowDirectionAny                                     animated:YES];}@end

2

in view controller that is source/input of segue e.g. start segue with action:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{    if(nil == self.recentSearchesPopoverController)    {        NSString *identifier = NSStringFromClass([CustomPopoverSegue class]);        [self performSegueWithIdentifier:identifier sender:self];    } }

3

references are assigned by segue which creates UIPopoverController - when dismissing popover

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{    if(self.recentSearchesPopoverController)    {        [self.recentSearchesPopoverController dismissPopoverAnimated:YES];        self.recentSearchesPopoverController = nil;    }    }

regards,Peter