swift – 当调用UIActivityViewController时,IOS 8 iPad应用程序崩溃

前端之家收集整理的这篇文章主要介绍了swift – 当调用UIActivityViewController时,IOS 8 iPad应用程序崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当在这个应用程序中在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)

}

正如您所看到的,我正在Swift中编程,在SpriteKit框架中,我不明白为什么应用程序崩溃.

我收到此错误

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

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

在演示UIActivityViewController之前,添加以下代码行:
activityViewController.popoverPresentationController?.sourceView = self.view

这样,视图控制器就知道要在哪个GameViewController框架中出现.

猜你在找的Swift相关文章