Search code examples
iosobjective-ccrashuipopovercontroller

iOS Crash: Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView


I need help figuring this crash out. I checked stackoverflow for answers, but non of the answers relates to my situation. This is my code.

  - (void)addImagesActionSheetClickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Cancel
    if (buttonIndex == 2) return;

    //Take picture
    if (buttonIndex == 0)
    {
        //Take picture
        isFromLibrary = NO;
        [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self.navigationController presentViewController:self.imagePicker animated:YES completion:NULL];
        return;
    }

    // Library picture
    if (buttonIndex == 1)
    {

    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) return;

    UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker];
    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

If I click button index 1 and dismiss the UIPopoverController then click button index 0 to take a picture my App crashes.

Here is my crash report

'Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController () should have a non-nil sourceView or barButtonItem set before the presentation occurs.

Any suggestions or tips are appreciated. If I need to post more code, please let me know.


Solution

  • You need to have a strong reference to popup

    @property (nonatomic, strong) UIPopoverController *popup;

    Then use

     - (void)addImagesActionSheetClickedButtonAtIndex:(NSInteger)buttonIndex
    {
        // Cancel
        if (buttonIndex == 2) return;
    
        //Take picture
        if (buttonIndex == 0)
        {
            //Take picture
            isFromLibrary = NO;
            [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            [self.navigationController presentViewController:self.imagePicker animated:YES completion:NULL];
            return;
        }
    
        // Library picture
        if (buttonIndex == 1)
        {
    
        if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) return;
    
            self.popup = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker];
            [self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }
    

    And implement UIPopoverControllerDelegate

    in

    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    
        self.popup = nil;
    
    }