我遇到了一个看起来大多数人在iPad上使用UIActivityViewController
时面临的情况;它会崩溃并显示以下错误:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc4f2d87d00>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.
这是我的代码:
- (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];
}
在我的viewDidLoad
中,我有:
UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareLeaflet)];
self.navigationItem.rightBarButtonItem = composeButton;
}
这个视图是一个 UIPageViewController
,展示了一些图片。当用户点击分享按钮时,我希望弹出 iOS 8 风格的共享面板。在 iPhone 上确实是这样的,但在 iPad 上它却不断崩溃。我在 Stack Overflow 上查找解决方法,但其中没有一个问题 (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等) 对我的情况适用。
我已经尝试了里面的所有解决方案,但还是不太理解需要什么操作。
这就是我想要实现的: