UIPopoverPresentationController

UIPopoverPresentationController

本文介绍了共享按钮在 iPhone 上完美运行,但在 iPad 上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个按钮,以便在 Twitter、Facebook 等中分享一些句子.它适用于所有 iPhone 型号,但模拟器在 iPad 上崩溃.

I'm trying to add a button in order to share some sentences in Twitter, Facebook... etc. It all works on all iPhone models but simulator crash with an iPad.

这是我的代码:

@IBAction func shareButton(sender: AnyObject) {

@IBAction func shareButton(sender: AnyObject) {

    frase = labelFrases.text!
    autor = labelAutores.text!


    var myShare = "\(frase) - \(autor)"

    let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)



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

这是错误:

由于未捕获的异常NSGenericException"而终止应用,原因:UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7c0f9190>) 应在演示发生前设置非 nil sourceView 或 barButtonItem

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

我该如何解决?谢谢

推荐答案

对于 ipad (iOS > 8.0) 需要设置 popoverPresentationController:

For ipad (iOS > 8.0) you need to set popoverPresentationController:

    //check ipad
    if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        //ios > 8.0
        if ( activityVC.respondsToSelector(Selector("popoverPresentationController")) ) {
            activityVC.popoverPresentationController?.sourceView = super.view
        }
    }

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

更多信息在这里:UIActivityViewController 在 iOS8 iPad 上崩溃

这篇关于共享按钮在 iPhone 上完美运行,但在 iPad 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:45