一尘不染

调用UIActivityViewController时,iOS 8 iPad应用程序崩溃

swift

当在此应用程序中的iPhone上调用UIActivityViewController时,它可以正常工作,但在iPad上调用时,该应用程序崩溃。下面是我使用的代码:

func shareButtonPress() {

    //when the share button is pressed, default share phrase is added, cropped image of highscore is added

    var sharingItems = [AnyObject]()

    var shareButtonHighscore = NSUserDefaults.standardUserDefaults().objectForKey("highscore") as Int!

    sharingItems.append("Just hit \(shareButtonHighscore)! Beat it! #Swath")

    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    sharingItems.append(image)

    let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

    var barButtonItem: UIBarButtonItem! = UIBarButtonItem()

    activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

如您所见,我正在使用SpriteKit Framework在Swift中进行编程,但我不明白为什么应用程序崩溃。

我收到此错误:

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

我该怎么做才能解决此问题?


阅读 411

收藏
2020-07-07

共1个答案

一尘不染

在展示之前UIActivityViewController,添加以下代码行:

activityViewController.popoverPresentationController?.sourceView = self.view

这样,视图控制器便知道要出现在GameViewController的哪个帧中。

2020-07-07