2

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.

William Alex
  • 385
  • 1
  • 3
  • 14
  • see here: https://stackoverflow.com/questions/42920340/uipopoverpresentationcontroller-should-have-a-non-nil-sourceview-or-barbuttonite/71193425#71193425 – ingconti Feb 20 '22 at 10:27

2 Answers2

1

Notice the discussion in the documentation :

sourceRect

Use this property in conjunction with the sourceView property to specify the anchor location for the popover. Alternatively, you may specify the anchor location for the popover using the barButtonItem property.

Pretty clear and concise. Just add a sourceView reference

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/sourceRect

soulshined
  • 9,612
  • 5
  • 44
  • 79
0

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;

}
Zahid
  • 552
  • 3
  • 11