问题
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.
回答1:
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;
}
回答2:
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
来源:https://stackoverflow.com/questions/28977348/ios-crash-terminating-app-due-to-uncaught-exception-reason-uipopoverpresentati