断言失败-[UIAlertController addTextFieldWithConfigurationHandler

Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:]

本文关键字:UIAlertController addTextFieldWithConfigurationHandler 失败 断言      更新时间:2023-09-26

在WKWebView中调用JavaScript中的window.prompt会产生断言错误:

断言失败-[UIAlertController .addTextFieldWithConfigurationHandler:]

断言错误来自这个WKUIDelegate函数:

func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
             completionHandler: @escaping (String?) -> Void) {
    let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)
    alertController.addTextField { (textField) in
        textField.text = defaultText
    }
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
        if let text = alertController.textFields?.first?.text {
            completionHandler(text)
        } else {
            completionHandler(defaultText)
        }
    }))
    alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
        completionHandler(nil)
    }))
    present(alertController, animated: true, completion: nil)
}

类文档没有显示在添加文本字段或初始化项时添加配置处理程序的方法。那你该怎么处理这件事?

尝试从样式actionSheet更改为alert。对于Objective-C用户,将从UIAlertControllerStyleActionSheet更改为UIAlertControllerStyleAlert

Present应该从viewController调用。

相关文章:
  • 没有找到相关文章