在 iOS8 中检测弹出框关闭

Posted

技术标签:

【中文标题】在 iOS8 中检测弹出框关闭【英文标题】:Detect popover dismiss in iOS8 【发布时间】:2014-10-21 07:25:25 【问题描述】:

在我的 iPad 应用程序中,我将一个 UIButton 连接到另一个 UIViewcontroller,只需拖动 Storyboard 并选择 popover 作为 segue。一切正常,但用户可以通过触摸弹出框右侧的某个位置来关闭弹出框。

如何在 ios8 中检测到弹出框已关闭?在 iOS7 中,我可以只使用 UIPopoverDelegate -popoverDidDidmiss ...

但这不再起作用了!?我google了很多,但找不到任何解决方案。

【问题讨论】:

【参考方案1】:

你把你的

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

在开始 UIButton 的 UIViewController 中? (不在另一个弹出窗口 UIViewcontroller 中?) 这对我来说适用于 iOS 8.1...

您必须为此委托给初始 UIViewController。

【讨论】:

【参考方案2】:

我假设您正确设置了委托,但是您是否保留了弹出框,即将它分配给一个强属性?在 ios7 中,如果您不保留弹出框,您会得到异常:'[UIPopoverController dealloc]达到弹出框仍然可见。在 ios8 中不再是这种情况,所以你得到了工作弹出框,你可以关闭它,但是不再调用委托方法。

(坦率地说,我不确定为什么会这样。我想至少应该调用“应该关闭弹出窗口”)。

【讨论】:

我不确定我是否正确。我是新手;)我没有弹出属性或分配它。从 UIButton 我将故事板拖到另一个 ViewController 并选择 popover 作为 segue。我以不同的方式解决了我的问题。在我的弹出窗口中有一个 UiTextfield,当用户关闭弹出窗口时我需要保存字符串。我通过听 textfieldDidEndEditing 实现了它。这对我有用并解决了我的问题,无论如何我无法调用 popoverDidDissmiss...谢谢帮助!【参考方案3】:

您可能应该使用UIPopoverControllerDelegate 和方法

popoverControllerDidDismissPopover:

完成你所需要的。

【讨论】:

正如我所说,这在 iOS7 中有效,但在 iOS8 中无效……但无论如何,谢谢【参考方案4】:

在 iOS8 中,它使用 UIViewController 的 UIPopoverPresentationController 来呈现弹出框。 (您仍然可以选择使用旧的UIPopoverController 手动构建弹出框。

如果您在 iOS8 上使用 storyboard,您可以设置 UIViewController 的 popoverPresentationController 委托来处理以下内容:

popoverPresentationControllerShouldDismissPopover: popoverPresentationControllerDidDismissPopover:

【讨论】:

以上是关于在 iOS8 中检测弹出框关闭的主要内容,如果未能解决你的问题,请参考以下文章

单击外部弹出框时如何防止 UIPopoverPresentationController 被关闭?

弹出框关闭后如何在 transitionCoordinator 中安排新的演示文稿

iOS8 在情节提要中设置弹出框大小

如何以编程方式关闭/禁用 iOS 11 中引入的屏幕截图弹出框?

iOS UITesting 如何关闭弹出框(iPad 弹出框不提醒样式)

iOS 弹出框在关闭后不会消失