[英]SwiftUI Custom TextField with UIViewRepresentable Issue with ObservableObject and pushed View
[英]Custom TextField keyboard in SwiftUI with UIViewRepresentable
我在 UIViewRepresentable 中使用自定義 UITextField,以擁有一個只有小數點和自定義鍵盤工具欄的文本字段。 目標是向工具欄添加基本內容,例如插入減號、E 等……我見過很多只針對“完成”按鈕的代碼。 我已經嘗試將 insertText() 添加到按鈕操作,但我認為沒有調用協調器,因此文本沒有更新。 我想要的只是在光標位置插入自定義字符串的能力。
這是代碼:
struct DataTextField: UIViewRepresentable {
private var placeholder: String
@Binding var text: String
init(_ placeholder: String, text: Binding<String>) {
self.placeholder = placeholder
self._text = text
}
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = .decimalPad
textfield.delegate = context.coordinator
textfield.placeholder = placeholder
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let minusButton = UIBarButtonItem(title: "-", style: .plain, target: self, action: #selector(textfield.minusButtonTapped(button:)))
let scientificButton = UIBarButtonItem(title: "E", style: .plain, target: self, action: #selector(textfield.scientificButtonTapped(button:)))
toolBar.items = [minusButton, scientificButton]
toolBar.setItems([minusButton, scientificButton], animated: true)
textfield.inputAccessoryView = toolBar
textfield.borderStyle = .roundedRect
textfield.textAlignment = .right
textfield.adjustsFontSizeToFitWidth = true
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: DataTextField
init(_ textField: DataTextField) {
self.parent = textField
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let currentValue = textField.text as NSString? {
let proposedValue = currentValue.replacingCharacters(in: range, with: string) as String
self.parent.text = proposedValue
}
return true
}
}
}
extension UITextField {
@objc func minusButtonTapped(button:UIBarButtonItem) -> Void {
insertText("-")
}
@objc func scientificButtonTapped(button:UIBarButtonItem) -> Void {
insertText("E")
}
}
以及問題的視頻:
我發現這是一個 Swift 錯誤,只有在文本不在數組內時才會更新文本。 (就我而言,它是)經過數小時的嘗試,我發現調用delegate?.textFieldDidBeginEditing?(self)
然后更新該函數內的文本會使按鈕按預期工作。
extension UITextField {
@objc func minusButtonTapped(button: UIBarButtonItem) -> Void {
insertText("-")
delegate?.textFieldDidBeginEditing?(self)
}
@objc func scientificButtonTapped(button: UIBarButtonItem) -> Void {
insertText("E")
delegate?.textFieldDidBeginEditing?(self)
}
}
將此添加到 UIViewRepresentable 內的 Coordinator 類:
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
textField.resignFirstResponder()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.parent.text = textField.text!
}
調用.resignFirstResponder()
是必須的。 如果鍵盤沒有關閉,應用程序將崩潰,因為在我的情況下,視圖本身可以被關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.