UIActivityViewController 在 iPad 上使用 sourceView 或 barButtonItem 崩溃 [英] UIActivityViewController crashing on iPad with sourceView or barButtonItem

查看:252
本文介绍了UIActivityViewController 在 iPad 上使用 sourceView 或 barButtonItem 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过大多数人在尝试在 iPad 上呈现 UIActivityViewController 时所面临的情况;它崩溃了:

由于未捕获的异常NSGenericException"而终止应用程序,原因:UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc4f2d87d00>) 应该在演示发生之前设置非 nil sourceView 或 barButtonItem.

这是我的代码:

- (void)shareLeaflet{NSString *forwardedString = [[NSString alloc] initWithFormat:@"查看这个传单\n\n %@ \n\nself.theURLToShare];UIActivityViewController *activityViewController = nil;如果(成语 == IPAD){NSLog(@"iPad");activityViewController.popoverPresentationController.sourceView = self.view;//activityViewController.popoverPresentationController.sourceRect = self.frame;[自我presentViewController:activityViewController动画:是完成:无];}别的{NSLog(@"iPhone");activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];[self presentViewController:activityViewController 动画:YES 完成:nil];}

在我的 viewDidLoad 中,我有:

UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareLeaflet)];self.navigationItem.rightBarButtonItem = composeButton;}

这个视图是一个 UIPageViewController,它展示了一些图像,当用户点击分享按钮时,我期待 iOS 8 样式分享表弹出.这正是 iPhone 上发生的情况,但在 iPad 上,它继续崩溃.这让我想到了 Stack Overflow,但没有任何问题(对此的任何想法将不胜感激.

解决方案

你没有在 iPad 上初始化 activityViewController,所以它总是为零.

试试:

- (void)shareLeaflet{NSString *forwardedString = [[NSString alloc] initWithFormat:@"查看这个传单\n\n %@ \n\nself.theURLToShare];UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];如果(成语 == IPAD){NSLog(@"iPad");activityViewController.popoverPresentationController.sourceView = self.view;//activityViewController.popoverPresentationController.sourceRect = self.frame;[自我presentViewController:activityViewController动画:是完成:无];}别的{NSLog(@"iPhone");[自我presentViewController:activityViewController动画:是完成:无];}

然后像在图像中一样显示它:(_shareBarButton 是您希望弹出框显示的 UIBarButtonItem)

- (void)shareLeaflet{NSString *forwardedString = [[NSString alloc] initWithFormat:@"查看这个传单\n\n %@ \n\nself.theURLToShare];UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];如果(成语 == IPAD){NSLog(@"iPad");activityViewController.popoverPresentationController.sourceView = self.view;//activityViewController.popoverPresentationController.sourceRect = self.frame;_popover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];_popover.delegate = self;[_popover presentPopoverFromBarButtonItem:_shareBarButton allowedArrowDirections:UIPopoverArrowDirectionAny animation:YES];}别的{NSLog(@"iPhone");[自我presentViewController:activityViewController动画:是完成:无];}

I have come across what looks like a situation that most people face when trying to present a UIActivityViewController on the iPad; it is crashing with:

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

Here's my code:

- (void)shareLeaflet
{
    NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare];
    UIActivityViewController *activityViewController = nil;

    if (IDIOM == IPAD)
    {
        NSLog(@"iPad");
        activityViewController.popoverPresentationController.sourceView = self.view;
//        activityViewController.popoverPresentationController.sourceRect = self.frame;
        [self presentViewController:activityViewController
                           animated:YES
                         completion:nil];

    }
    else
    {
        NSLog(@"iPhone");
        activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];
        [self presentViewController:activityViewController animated:YES completion:nil];


    }

In my viewDidLoad, I have:

UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self                                    action:@selector(shareLeaflet)];

    self.navigationItem.rightBarButtonItem = composeButton;
}

This view is a UIPageViewController which is showcasing some images and when the user hits the share button, I'm expecting the iOS 8 style share sheet to popup. This is exactly what happens on the iPhone, but on the iPad, it continues to crash. That led me to Stack Overflow, but none of the questions (crash on showing UIPopOverPresentationController, iOS Crash: Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView, UIWebViewTerminating app due to UIPopoverPresentationController, ios8 iPad uiwebview crashes while displaying popover when user taps drop down list HTML select tag, etc) work for me.

I have tried all of the solutions in there and I just quite get what is required with this.

This is what I'm trying to achieve:

Any thoughts on this would be really appreciated.

解决方案

You aren't initialising the activityViewController on iPad so it will always be nil.

Try:

- (void)shareLeaflet
{
    NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];

    if (IDIOM == IPAD)
    {
        NSLog(@"iPad");
        activityViewController.popoverPresentationController.sourceView = self.view;
//        activityViewController.popoverPresentationController.sourceRect = self.frame;
        [self presentViewController:activityViewController
                           animated:YES
                         completion:nil];
    }
    else
    {
        NSLog(@"iPhone");
        [self presentViewController:activityViewController 
                          animated:YES 
                        completion:nil];
    }

And then to display it like in the image: (_shareBarButton is the UIBarButtonItem that you want the popover to display from)

- (void)shareLeaflet
    {
        NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare];
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil];

        if (IDIOM == IPAD)
        {
            NSLog(@"iPad");
            activityViewController.popoverPresentationController.sourceView = self.view;
    //        activityViewController.popoverPresentationController.sourceRect = self.frame;

           _popover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
           _popover.delegate = self;
           [_popover presentPopoverFromBarButtonItem:_shareBarButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
        else
        {
            NSLog(@"iPhone");
            [self presentViewController:activityViewController 
                              animated:YES 
                            completion:nil];
        }

这篇关于UIActivityViewController 在 iPad 上使用 sourceView 或 barButtonItem 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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