is there a way NOT to have the popover dismissed when pressing outside it?
Asked Answered
R

3

9

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];
Rizzio answered 29/3, 2011 at 18:49 Comment(0)
M
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.

Malo answered 29/3, 2011 at 18:54 Comment(1)
Thanks! I am going to try it right away. I'm ashamed I couldn't find it myself in the code, but that's why we have the stackoverflow, right? :) Do you have an example/reference to example for how to set these views?Rizzio
E
10

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.

Endbrain answered 15/8, 2013 at 16:1 Comment(0)
M
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.

Machree answered 14/10, 2011 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.