断言失败-[UIAlertController addTextFieldWithConfigurationHandler
Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:]
在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调用。
相关文章:
- 没有找到相关文章