简体   繁体   中英

UIPopoverController and UIImagePickerController crash

Here is the set up of my view:

查看设定

When the UIBarButtonItem is clicked, it should bring up a 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. 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.'

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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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