在 iPad 中呈现 UIActivityController 时应用崩溃

App crashes when presenting UIActivityController in iPad

我已经在我的应用程序中创建了一个视图,单击按钮后,UIActivityController 应该会在屏幕上弹出。但是,应用程序仅在 iPad 上崩溃,但在 iPhone 上运行正常。任何人都知道这是什么行为以及如何解决它?这是我使用的代码:

@IBAction func send(_ sender: AnyObject)
{        
    var image: UIImage!
    image = UIImage(named: "image.png")
    var urlfinal:NSURL!
    urlfinal =  URL.init(string:url as String)! as NSURL
    let items = [mytitle , image , urlfinal] as [Any]


    let controller = UIActivityViewController(activityItems: items, applicationActivities: .none)

    controller.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem

    self.present(controller, animated: true, completion: nil)
}

这是我收到的控制台错误:

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

如错误提示,您需要将 sourceView 添加到 popoverPresentationController

controller.popoverPresentationController?.sourceView = sender as! UIView