【问题标题】:UIAlertController and displayShareSheet crashing on iPadUIAlertController 和 displayShareSheet 在 iPad 上崩溃
【发布时间】: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


    【解决方案1】:

    替换这些行:

    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 作为参数传递给该方法,以便您可以在那里使用它。

    【讨论】:

    • 考虑到我的按钮是 UIButton 而不是 UIBarButtonItem,你会修复仍然有效吗?我已经有了一个出口——@IBOutlet weak var uploadBtn: UIButton!
    • 糟糕。我以为是UIBarButtonItem。查看我对UIButton 的更新。
    • 这对 alertController 非常有用!它定位正确。 “显示共享表”仍在崩溃。我不确定如何以相同的方式配置共享表?
    • 我已经在我的回答中告诉过你。将按钮传递给您的 displayShareSheet 方法,并将与您对警报所做的相同的修复应用于共享表。
    • 谢谢!完美
    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多