8

I know the SDK documentation says

Taps outside of the popover’s contents automatically dismiss the popover.

But I'm sure the smart people here found a way :) maybe I should overwrite the popover dismiss function?

Thanks

EDIT: I tried using the passthroughViews as was suggested here, and it works perfectly. Here's the code for whoever needs it - in this example, I put self.view in the array, which means that where ever outside the button where the popover was originated, nothing dismiss the popover.

        popoverController.passthroughViews = [[[NSArray alloc] initWithObjects:self.view, nil] autorelease];
4

3 回答 3

13

You need to set the passthroughViews property. From the documentation:

An array of views that the user can interact with while the popover is visible.

@property (nonatomic, copy) NSArray *passthroughViews

When a popover is active, interactions with other views are normally disabled until the popover is dismissed. Assigning an array of views to this property allows taps outside of the popover to be handled by the corresponding views.

Set passthroughViews to an array of view(s) that you want to handle the touch event instead of just dismissing the popover.

于 2011-03-29T18:54:08.107 回答
9

There is a very simple and legit solution. In the view controller that presents your UIPopoverController, conform to the UIPopoverControllerDelegate protocol and implement the following delegate method. I just tested this and it does prevent popover to dismiss.

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    return NO;
}

Just make sure that you have set the delegate of your popover controller to the view controller that implements this.

You can dismiss the popover by using [popoverController dismissPopoverAnimated:NO]; method.

于 2013-08-15T16:01:17.337 回答
7

The accepted answer does not really answer the question, "is there a way NOT to have the popover dismissed when pressing outside it?", imo. It does give a possible view but could require hackish access to all parent views and determining what views are on the screen etc. The question could be rephrased as, "how do I make a popover view modal?"

You would do this like so, with a done button to close the popover:

UIViewController* vc = [[[UIViewController alloc] init] autorelease];

UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(processDoneAction)] autorelease];

[vc.navigationItem setLeftBarButtonItem:doneButton];

vc.modalInPopover = YES;
//If you want full screen:
vc.modalPresentationStyle = UIModalPresentationFullScreen;
vc.wantsFullScreenLayout = YES;

UINavigationController* navC = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];

UIView* view = create your view

vc.view = view;

UIPopoverController* pc = [[[UIPopoverController alloc] initWithContentViewController:navC] autorelease];
pc.delegate = self;
self.popoverController = pc;

Then you'll in your processDoneAction method you will need to dismiss the popover. Other considerations would be dismissing and redisplaying on device orientation changes, but I will leave that to another exercise as that has been answered previously on stackoverflow.

于 2011-10-14T15:46:38.417 回答