gpt4 book ai didi

ios - UIAlertController 和 displayShareSheet 在 iPad 上崩溃

转载 作者:行者123 更新时间:2023-11-28 16:07:50 25 4
gpt4 key购买 nike

我在 View 中有一个 UIButton。 UIButton 的位置类似于

enter image description here

我有一个 @IBAction,它在单击 UIButton 时触发

@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)

}

这会产生如下所示的警报

enter image description here

选择“共享报价”时,警报会提出一个sharesheet。

这个@IBAction 在 iPhone 上工作,但在 iPad 上崩溃。错误信息是

'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

我试图通过尝试类似的方法来解决这个问题

    //iPad

alertController.popoverPresentationController?.sourceView = viewBottom
alertController.popoverPresentationController?.sourceRect = viewBottom.bounds
alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down;

此修复程序不起作用。警报错误地定位在“viewBottom”上方,当我单击“defaultAction”按钮时 - 它再次崩溃并显示上述错误消息。

我有点不知如何解决这个问题。任何人都可以对此提出一些建议吗?我尝试过各种使用方式

the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

在我的代码中,但没有成功。任何建议对此表示赞赏。谢谢。

最佳答案

替换这些行:

alertController.popoverPresentationController?.sourceView = viewBottom
alertController.popoverPresentationController?.sourceRect = viewBottom.bounds
alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down;

与:

if let button = sender as? UIButton {
alertController.popoverPresentationController?.sourceView = button
alertController.popoverPresentationController?.sourceRect = button.bounds
}

您还必须在 displayShareSheet 方法中设置共享表的 popoverPresentationController。您可能应该将 UIButton 作为参数传递给该方法,以便您可以在那里使用它。

关于ios - UIAlertController 和 displayShareSheet 在 iPad 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39982484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com