iOS 崩溃:由于未捕获的异常原因而终止应用程序:UIPopoverPresentationController 应该有一个非零的 sourceView

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

我需要帮助来解决这个崩溃问题。我检查了 Whosebug 的答案,但没有一个答案与我的情况有关。这是我的代码。

  - (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];
    }
}

如果我单击按钮索引 1 并关闭 UIPopoverController 然后单击按钮索引 0 拍照,我的应用程序崩溃了。

这是我的崩溃报告

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

如有任何建议或提示,我们将不胜感激。如果我需要 post 更多代码,请告诉我。

注意文档中的讨论:

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.

非常清晰简洁。只需添加一个 sourceView 引用

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

你需要对popup

有很强的参考

@property (nonatomic, strong) UIPopoverController *popup;

然后使用

 - (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];
    }
}

并实施UIPopoverControllerDelegate

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{

    self.popup = nil;

}