【发布时间】:2017-02-20 07:17:56
【问题描述】:
我在视图中有一个 UIButton。 UIButton 的位置类似于
我有一个在单击 UIButton 时触发的@IBAction
@IBAction func shareButtonClicked(_ sender: AnyObject) {
Flurry.logEvent("Share button tapped");
let textToShare = quotetext.text
// 1.
// Create and initialize a UIAlertController instance.
//
let alertController = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
// 2.
// Initialize the actions to show along with the alert.
//
let copyAction = UIAlertAction(title:"Copy Quote to clipboard",
style: .default) { (action) -> Void in
let pasteboard: UIPasteboard = UIPasteboard.general
pasteboard.string = self.quotetext.text;
}
let defaultAction = UIAlertAction(title:"Share Quote",
style: .default) { (action) -> Void in
// We have contents so display the share sheet
self.displayShareSheet(shareContent: textToShare)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// ...
}
// 3.
// Tell the alertController about the actions we want it
// to present.
//
alertController.addAction(copyAction)
alertController.addAction(defaultAction)
alertController.addAction(cancelAction)
// 4.
// Present the alert controller and associated actions.
//
self.present(alertController, animated: true, completion: nil)
}
这会产生一个看起来像
的警报选择“共享报价”时,警报会带来Sharesheet。
此@IBAction 在 iPhone 上有效,但在 iPad 上崩溃。错误信息是
'NSGenericException', reason: '你的应用程序已经提交了一个 UIAlertController() 的样式 UIAlertControllerStyleActionSheet。 modalPresentationStyle 的 这种风格的 UIAlertController 是 UIModalPresentationPopover。你 必须通过警报提供此弹出框的位置信息 控制器的 popoverPresentationController。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果这个信息是 当您出示警报控制器时不知道,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation。'
我试图通过尝试类似的方法来解决这个问题
//iPad
alertController.popoverPresentationController?.sourceView = viewBottom
alertController.popoverPresentationController?.sourceRect = viewBottom.bounds
alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down;
此修复不起作用。警报在“viewBottom”上的位置不正确,当我单击“defaultAction”按钮时 - 它再次崩溃并显示上述错误消息。
我有点不知所措,无法解决这个问题。任何人都可以对此提出一些建议吗?我尝试了各种使用方式
UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation。'
在我的代码中,但没有成功。对此表示赞赏的任何建议。谢谢。
【问题讨论】:
标签: ios swift xcode ipad uibutton