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

喜夏-厌秋 提交于 2019-11-30 13:03:39

问题


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];

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/5477422/is-there-a-way-not-to-have-the-popover-dismissed-when-pressing-outside-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!