UIActivity activityViewController 在 Swift 中的 iPad 崩溃
UIActivity activityViewController crash on iPad in Swift
我有下面的代码来切换 UIActivityViewController
@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"
if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
但它在 iPad 上崩溃了。我如何在 Swift?
中模态显示它
它会崩溃,因为在 iPad 中你不能像在 iPhone 中那样呈现 UIActivityViewController
。
您需要按照 Apple 建议的设计指南将其呈现在弹出窗口中。
下面的代码将演示如何在 UIPopoverPresentationController
.
中显示它
@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"
if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
var nav = UINavigationController(rootViewController: activityVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController as UIPopoverPresentationController!
activityVC.preferredContentSize = CGSizeMake(500,600)
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
}
注意:使用 UIPopoverPresentationController
委托扩展您的视图控制器。
例如:
class MyViewController : UIViewController, UIPopoverPresentationControllerDelegate {
//........
}
我有下面的代码来切换 UIActivityViewController
@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"
if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
但它在 iPad 上崩溃了。我如何在 Swift?
中模态显示它它会崩溃,因为在 iPad 中你不能像在 iPhone 中那样呈现 UIActivityViewController
。
您需要按照 Apple 建议的设计指南将其呈现在弹出窗口中。
下面的代码将演示如何在 UIPopoverPresentationController
.
@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"
if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
var nav = UINavigationController(rootViewController: activityVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController as UIPopoverPresentationController!
activityVC.preferredContentSize = CGSizeMake(500,600)
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
}
注意:使用 UIPopoverPresentationController
委托扩展您的视图控制器。
例如:
class MyViewController : UIViewController, UIPopoverPresentationControllerDelegate {
//........
}