ios - 在 iPad 上共享工作表崩溃

标签 ios iphone swift ipad

我正在创建一个共享表,人们可以在其中共享文本字段中的文本。我已经在 iPhone 上运行它,但该应用程序在 iPad 上崩溃了。我知道它必须采用弹出窗口形式,并且在 Objective-C 时代就遇到过这个问题,但我似乎无法在 Swift 上解决这个问题。这是我为共享表准备的代码:

@IBAction func myShareButton(sender: AnyObject) {
        // Hide the keyboard
        textView.resignFirstResponder()
        // Check and see if the text field is empty
        if (textView.text == "") {
            // The text field is empty so display an Alert
            displayAlert("Warning", message: "You haven't written anything yet!")
        } else {
            // We have contents so display the share sheet
            displayShareSheet(textView.text!)
        }
    }
    // Show Warning
    func displayAlert(title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(alertController, animated: true, completion: nil)
        return
    }
    // Display Share Sheet
    func displayShareSheet(shareContent:String) {
        let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
        presentViewController(activityViewController, animated: true, completion: {})

    }

最佳答案

我今天也实现了共享表,但遇到了完全相同的问题。您需要在演示之前添加它。

if let popoverPresentationController = activityController.popoverPresentationController {
     popoverPresentationController.sourceView = self.view
     popoverPresentationController.sourceRect = CGRect(x: CGRectGetMidX(view.bounds), y: CGRectGetMidY(view.bounds), width: 0, height: 0)
     popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0) //Removes arrow as I dont want it
}

第 1 行设置源 View 。

第 2 行我用来将弹出框居中(我在 SpriteKit 中使用它并且弹出框没有附加到任何东西)

第 3 行我用来删除箭头,因为我不想要它。

希望对你有帮助

关于ios - 在 iPad 上共享工作表崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625547/

相关文章:

iphone - 在 iPhone 应用程序中打印 html 页面

ios - CarPlay 与外部配件的关系和无线配件配置

iphone - 本地通知声音不起作用

ios - 导航到苹果 map 上的当前位置

ios - 如何访问静态单元格中的文本标签

iphone - 如何在 iphone 中增加和调整 tableview 单元格的大小?

ios - UILabel 不会使用 AutoLayout 在 UIScrollView 内自动换行

iphone - 将应用程序升级到 iPhone 4 : how to get touches to give low-res screen values

ios - 有没有办法将 CSR 解码为 String 以获取属性?

swift - 将 View Controller 推到 NavigationController 后,导航标题未显示在中心