如何解决尝试为自定义 AlertControlView 设置委托
我的警报代码如下:
res = '<a href="#" data-id="btnedit" data-personid="' + row[6] + '" data-toggle="tooltip" data-placement="top" title="" class="btn-get-row btn btn-primary"><i class="fa fa-pencil"></i></a>';
$(document).ready( () => {
$('.btn-get-row').click( (e) => {
let myCells = $(this).closest('tr').find('td');
let data = {
description: myCells[0].innerHTML,// or myCells[0].find('input').val();
name: myCells[1].innerHTML // or myCells[1].find('input').val();
// ... and so on
}
})
})
它被这样称呼:
struct AlertControlView: UIViewControllerRepresentable {
@Binding var textString: String
@Binding var showAlert: Bool
var title: String
var message: String
// Make sure that,this fuction returns UIViewController,instead of UIAlertController.
// Because UIAlertController gets presented on UIViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<AlertControlView>) -> UIViewController {
return UIViewController() // Container on which UIAlertContoller presents
}
func updateUIViewController(_ uiViewController: UIViewController,context: UIViewControllerRepresentableContext<AlertControlView>) {
// Make sure that Alert instance exist after View's body get re-rendered
guard context.coordinator.alert == nil else { return }
if self.showAlert {
// Create UIAlertController instance that is gonna present on UIViewController
let alert = UIAlertController(title: title,message: message,preferredStyle: .alert)
context.coordinator.alert = alert
// Adds UITextField & make sure that coordinator is delegate to UITextField.
alert.addTextField { textField in
textField.placeholder = "Enter some text"
textField.text = self.textString // setting initial value
textField.delegate = context.coordinator // using coordinator as delegate
}
// As usual adding actions
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel",comment: ""),style: .destructive) { _ in
// On dismiss,SiwftUI view's two-way binding variable must be update (setting false) means,remove Alert's View from UI
alert.dismiss(animated: true) {
self.showAlert = false
}
})
alert.addAction(UIAlertAction(title: NSLocalizedString("Submit",style: .default) { _ in
// On submit action,get texts from TextField & set it on SwiftUI View's two-way binding varaible `textString` so that View receives enter response.
if let textField = alert.textFields?.first,let text = textField.text {
self.textString = text
}
alert.dismiss(animated: true) {
self.showAlert = false
}
})
// Most important,must be dispatched on Main thread,// CurIoUs? then remove `dispatchQueue.main.async` & find out yourself,Dont be lazy
dispatchQueue.main.async { // must be async !!
uiViewController.present(alert,animated: true,completion: {
self.showAlert = false // hide holder after alert dismiss
context.coordinator.alert = nil
})
}
}
}
func makeCoordinator() -> AlertControlView.Coordinator {
Coordinator(self)
}
class Coordinator: NSObject,UITextFieldDelegate {
// Holds reference of UIAlertController,so that when `body` of view gets re-rendered so that Alert should not disappear
var alert: UIAlertController?
// Holds back reference to SwiftUI's View
var control: AlertControlView
init(_ control: AlertControlView) {
self.control = control
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.text = textField.text!.replacingOccurrences(of: "^0+",with: "")
}
func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool {
if let text = textField.text as Nsstring? {
self.control.textString = text.replacingCharacters(in: range,with: string)
} else {
self.control.textString = ""
}
return true
}
}
}
我似乎无法调用 shouldChangeCharactersIn 函数,或者任何 begin、end 或 onchange 函数?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。