我正在创建一个共享表,人们可以在其中共享文本字段中的文本。我已经在 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/