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

9

在这个应用程序中,当在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.'

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

我该怎么做才能解决这个问题?-- 1. 阅读错误信息。2. 查看关于UIPopoverController的文档。 - Mundi
5个回答

31
在呈现UIActivityViewController之前,请添加以下代码行:
activityViewController.popoverPresentationController?.sourceView = self.view

这样,视图控制器就知道在GameViewController的哪个框架中显示。


4

如果您阅读错误提示,它会告诉您如何解决问题。您需要设置barButtonItem或sourceView,以便从中呈现popover,在您的情况下:

func shareButtonPress(pressedButton: UIBarButtonItem) { 

    ...

    activityViewController.popoverPresentationController.barButtonItem = pressedButton

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

现在我遇到了这个错误:[MyGame.GameViewController shareButtonPress]: unrecognized selector sent to instance 0x15f8c610' - tdh
我该怎么做才能纠正这个问题?我无法用实际按钮的名称替换UIBarButtonItem,因为实际按钮是在GameScene中定义的。 - tdh
您如何将目标添加到UIBarButtonItem中? - Steve
这是比其他方案更好的解决方案,因为此视图控制器应显示在“调用者”视图的顶部。 - artem

2

Swift 5:

检查设备是否为或,并基于此添加sourceView并呈现activityController

let activity = UIActivityViewController(activityItems: [self], applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .phone {
    UIApplication.topViewController?.present(activity, animated: true, completion: nil)
} else {
    activity.popoverPresentationController?.sourceView = UIApplication.topViewController!.view
    UIApplication.topViewController?.present(activity, animated: true, completion: nil)
}

1

有两个选项,这个操作是来自一个UIBarButtonitem或者UIButton,它们都是UIView。

func shareButtonPress() {

    ...

    if let actv = activityViewController.popoverPresentationController {
        actv.barButtonItem = someBarButton // if it is a UIBarButtonItem

        // Or if it is a view you can get the view rect
        actv.sourceView = someView
        // actv.sourceRect = someView.frame // you can also specify the CGRect
    }

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

您可能需要在函数中添加一个发送者,例如func shareButtonPress(sender: UIBarButtonItem)func shareButtonPress(sender: UIButton)


1
我为Swift 3添加了以下内容:

activityViewController.popoverPresentationController?.sourceView = self.view

固定了我的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接