iOS崩溃:由于未捕获异常原因终止应用程序:UIPopoverPresentationController应该有一个非nil的sourceView [英] iOS Crash: Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView

查看:1106
本文介绍了iOS崩溃:由于未捕获异常原因终止应用程序:UIPopoverPresentationController应该有一个非nil的sourceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助确定此崩溃。我检查stackoverflow的答案,但没有答案涉及到我的情况。这是我的代码。

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

如果我点击按钮索引1并关闭 UIPopoverController ,然后点击按钮索引0拍摄我的应用程序崩溃的图片。

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

这是我的崩溃报告


'由于未捕获异常而终止应用程序' NSGenericException',
reason:'UIPopoverPresentationController
()应该在展示之前设置一个非nil
sourceView或barButtonItem。

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

推荐答案

您需要有一个强大的引用 popup

You need to have a strong reference to 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

And implement UIPopoverControllerDelegate

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

    self.popup = nil;

}

这篇关于iOS崩溃:由于未捕获异常原因终止应用程序:UIPopoverPresentationController应该有一个非nil的sourceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆