UIPopoverController & UIImagePickerControl:“无法从没有窗口的视图中呈现弹出窗口”

发布于 2024-09-28 22:37:10 字数 774 浏览 1 评论 0原文

我正在尝试在我的 iPad 应用程序中显示 UIImagePickerControl。起初,调试器告诉我,在 iPad 上执行此操作时,需要将其放入弹出窗口中。所以我编写了以下代码:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) 
                         inView:self.view
       permittedArrowDirections:UIPopoverArrowDirectionAny 
                       animated:YES];

但是,现在我收到以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法从没有窗口的视图中呈现弹出窗口。”

关于我应该做什么有什么建议吗?我知道 self.view 应该 有一个窗口,但显然......它没有?

I am trying to display a UIImagePickerControl in my iPad app. At first, the debugger told me that I needed to put it in a popover when doing it on an iPad. So I wrote the following code:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) 
                         inView:self.view
       permittedArrowDirections:UIPopoverArrowDirectionAny 
                       animated:YES];

However, now I get the following error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a view which does not have a window.'

Any suggestions on what I should do? I know that self.view should have a window, but apparently... it doesn't?

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

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

发布评论

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

评论(2

北方的巷 2024-10-05 22:37:10

如果在加载视图之前执行那段代码,则可能会发生这种情况,因为 self.view 仍然为零,因此 self.view.window 也是如此。

您是否有可能在加载视图之前(在调用 -viewDidLoad: 之前)在 init 方法或其他地方执行此操作?

This can happen if that bit of code is getting executed before the view is loaded, as self.view is still nil and therefore so is self.view.window.

Is it possible that you're doing this in an init method or some other place before the view is loaded (before -viewDidLoad: is called)?

霞映澄塘 2024-10-05 22:37:10

这个错误让我感到困惑,但事实证明,这是因为我传递给 presentPopoverFromBarButtonItembarButtonItem 是一个 UIBarButtonSystemItemFlexibleSpace 项,它是一个实际的按钮。我的用户错误,但只是为了让人们知道这也可以

This error had me baffled but it turned out that it was because the barButtonItem I was passing in to presentPopoverFromBarButtonItem was a UIBarButtonSystemItemFlexibleSpace item ant an actual button. My user error but just so people know this can als

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