有没有办法在按下弹出框外部时不让弹出框消失?

发布于 2024-10-27 23:49:37 字数 389 浏览 8 评论 0原文

我知道 SDK 文档说

点击弹出窗口内容之外的区域会自动关闭弹出窗口。

但我确信这里的聪明人找到了方法:) 也许我应该覆盖弹出窗口关闭功能?

谢谢

编辑: 我尝试按照此处的建议使用 passthroughViews,并且效果完美。下面是任何需要它的人的代码 - 在这个示例中,我将 self.view 放入数组中,这意味着无论在弹出窗口起源的按钮之外,都不会关闭弹出窗口。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-11-03 23:49:37

您需要设置 passthroughViews 属性。从文档中:

在弹出窗口可见时用户可以与之交互的视图数组。

@property(非原子,复制)NSArray *passthroughViews

当弹出窗口处于活动状态时,通常会禁用与其他视图的交互,直到弹出窗口被关闭为止。将视图数组分配给此属性允许相应视图处理弹出窗口外部的点击。

passthroughViews 设置为您想要处理触摸事件而不是仅仅关闭弹出窗口的视图数组。

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.

梦魇绽荼蘼 2024-11-03 23:49:37

有一个非常简单且合法的解决方案。在呈现 UIPopoverController 的视图控制器中,遵循 UIPopoverControllerDelegate 协议并实现以下委托方法。我刚刚测试了这个,它确实阻止了弹出窗口的关闭。

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

只需确保您已将弹出窗口控制器的委托设置为实现此功能的视图控制器。

您可以使用 [popoverController DismissPopoverAnimated:NO]; 方法关闭弹出窗口。

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.

温馨耳语 2024-11-03 23:49:37

接受的答案并没有真正回答这个问题,“有没有办法在按下弹出框之外时不让弹出框消失?”,我认为。它确实提供了一个可能的视图,但可能需要对所有父视图进行黑客访问并确定屏幕上有哪些视图等。这个问题可以改写为“如何制作弹出视图模式?”

您可以像这样执行此操作,使用完成按钮来关闭弹出窗口:

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;

然后您将在 processDoneAction 方法中需要关闭弹出窗口。其他考虑因素是在设备方向更改时忽略并重新显示,但我将把它留给另一个练习,因为之前已经在 stackoverflow 上回答过。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文