从模态视图控制器呈现弹出窗口
我正在尝试通过 UIModalPresentationPageSheet 中的按钮呈现 Popover。它使应用程序崩溃并向控制台返回以下消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a view which does not have a window.'
我的代码在标准视图控制器中运行良好。正如消息所暗示的那样,模态中存在一些阻止其显示的因素。我已经对这个主题进行了大量搜索,但没有找到解决方案。我必须相信这是可能的,因为我在 1Password 等应用程序中看到过非常类似的东西。
非常感谢指点和/或建议。
I am trying to present a Popover from a button in a UIModalPresentationPageSheet. It crashes the app and returns the following message to the console:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a view which does not have a window.'
My code works fine in a standard view controller. As the message suggests, there is something about the modal that prevents it displaying. I've done a fair amount of searching on this topic, but am not finding a solution. I have to believe it is possible, as I've seen something very similar in apps like 1Password.
Pointers and/or suggestions greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的代码似乎对我有用,试试这个:
The following code seems to be working for me, try this :
我不知道你使用的是哪个 SDK,但我在 5.1 中没有遇到任何异常。我正在显示文本字段中的弹出窗口,并且以下代码有效:
}
I don't know which SDK did you use, but I don't get any exception with 5.1. I am showing a popover from a text field and the following code works:
}
我敢打赌问题是您传递的是视图控制器而不是 inView 参数的视图。它试图为 UIViewController 获取 .window,而 UIViewController 没有,而 UIView 有。
I would bet that the problem is that you're passing a view controller instead of a view for the inView parameter. It's trying to fetch the .window for a UIViewController, and UIViewControllers don't have one, UIViews do.