简体   繁体   English

UIPopoverController和UIImagePickerController崩溃

[英]UIPopoverController and UIImagePickerController crash

Here is the set up of my view: 这是我的观点的设置:

查看设定

When the UIBarButtonItem is clicked, it should bring up a UIImagePickerController . 单击UIBarButtonItem ,应调出一个UIImagePickerController I have to do this using a UIPopoverController , which is invoked by clicking on the "reset" button, because it is necessary on the iPad. 我必须使用UIPopoverController来执行此操作,这是通过单击“重置”按钮来调用的,因为在iPad上这是必需的。 Here is my code: 这是我的代码:

-(IBAction) btnReset:(id)sender {
    [self chooseImage];
}

-(void) chooseImage {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        imagepicker = [[UIImagePickerController alloc] init];
        imagepicker.allowsEditing = NO;
        imagepicker.delegate = self;
        imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagepicker.navigationBar.opaque = true;


        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];

            [popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

        } else {
            [self presentModalViewController:imagepicker animated:YES];   
        }
    }
}

However, when this is called, the view crashes with the error: 但是,调用此方法时,视图崩溃并显示以下错误:

'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.' 'NSInvalidArgumentException',原因:'-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]:无法从没有窗口的视图中显示弹出窗口。

What am I doing wrong? 我究竟做错了什么? Thank you in advance. 先感谢您。

It looks like you are trying to create a popover on an item which is not on the view hierarchy. 似乎您正在尝试在不在视图层次结构上的项目上创建弹出框。 If this method is being invoked by your button then change the method header to -(void) chooseImage:(id)sender and try presenting the popover from the UIBarButton you have on your toolbar. 如果您的按钮正在调用此方法,则将方法标题更改为-(void)choiceImage:(id)sender,然后尝试从工具栏上的UIBarButton呈现弹出窗口。

Also if you are using ARC (which it looks like you are) you have to hold on to your UIPopover otherwise it will be released when it is still required see this stack overflow post . 另外,如果您正在使用ARC(看起来像您一样),则必须坚持使用UIPopover,否则在仍需要它时将其释放,请参见此堆栈溢出文章 You may already be doing this but I thought I would raise it as a I can't see if/how you have specified your popoverController. 您可能已经在执行此操作,但我认为我会提出,因为我无法确定您是否/如何指定了popoverController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM