UIPopoverController和UIImagePickerControl:"无法从没有窗口的视图中显示弹出窗口"

Jas*_*son 12 uiimagepickercontroller ipad uipopovercontroller ios

我想在我的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];
Run Code Online (Sandbox Code Playgroud)

但是,现在我收到以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a view which does not have a window.'

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

Jay*_*yer 13

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

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