ios 无法在onCommit函数中清理TextField

vom3gejh  于 2023-05-02  发布在  iOS
关注(0)|答案(4)|浏览(131)

我是UIKit的SwiftUI新手,我有一个关于TextField行为的问题。

struct ContentView: View {
    @State private var text = ""
    @State private var words: [String] = []

    var body: some View {
        Form {
            Section {
                TextField("Input", text: $text) {
                    words.insert(text, at: 0)
                    text = ""
                }
            }
            
            Section {
                Button("Clear") {
                    text = ""
                }
            }
            
            Section {
                ForEach(words, id: \.self) { word in
                    Text(word)
                }
            }
        }
    }
}

我想做的行为是清除文本并将其添加到列表中。输入后,文本字段将被清除。现在的问题是调用了text = "",但它没有清理字段。但是,通过在下面有一个单独的按钮,它可以正常工作。
对于上下文,我需要将最低部署版本设置为iOS14,并且我使用的是Xcode 14。0.1.

我试着把它移到一个功能,但也没有帮助。

pgvzfuti

pgvzfuti1#

text = ""之前的清除按钮中,只需在数组中添加

struct ContentView: View {
    @State private var text = ""
    @State private var words: [String] = []

    var body: some View {
        Form {
            Section {
                TextField("Input", text: $text) {
                    words.insert(text, at: 0)
                    text = ""
                }
            }
            
            Section {
                Button("Clear") {
                    words.append(text) // <-- here append first and then clear.
                    text = ""
                }
            }
            
            Section {
                ForEach(words, id: \.self) { word in
                    Text(word)
                }
            }
        }
    }
}

xcitsw88

xcitsw882#

对于iOS 14,可以通过UITextField的UIViewRepresentableCoordinator类来管理文本字段委托。

下面是相同的工作代码。

struct TextFieldView: View {

   @State private var text = ""
   @State private var words: [String] = []

   var body: some View {
        Form {
            Section {
                //Use here UIViewRepresentable class of TextField
                CustomTextFieldView(inputText: $text, arrWords: $words, placeHolderText: "Input Text")
            }
        
            Section {
                ForEach(words, id: \.self) { word in
                    Text(word)
                }
            }
        }
    }
} 

struct CustomTextFieldView: UIViewRepresentable {

   @Binding var inputText: String
   @Binding var arrWords: [String]

   var placeHolderText: String

   func makeUIView(context: Context) -> UITextField {
       let textView = UITextField()
       textView.placeholder = placeHolderText
       textView.delegate = context.coordinator
       textView.autocapitalizationType = .sentences

       return textView
   }

   func updateUIView(_ uiView: UITextField, context: Context) {
       uiView.text = inputText
   }

   func makeCoordinator() -> Coordinator {
       Coordinator($inputText, textArr: $arrWords)
   }
 
   class Coordinator: NSObject, UITextFieldDelegate {
      var text: Binding<String>
      var arrText: Binding<[String]>
 
      init(_ text: Binding<String>, textArr : Binding<[String]>) {
         self.text = text
         self.arrText = textArr
      }
 
      func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         arrText.wrappedValue.append(textField.text!)
         textField.text = ""
         return true
      }
   }
}

******在iOS 15***及以上版本***中,可以通过TextField的onCommit方法进行管理

TextField("Input", text: $text).onSubmit {
    words.insert(text, at: 0)
    text = ""
}
lf5gs5x2

lf5gs5x23#

onCommit在onEditingChanged之前触发,可能这就是为什么会有竞争问题。与onCommit不同,onSubmit在onEditingChanged之后触发。当需要更改绑定值时,它可以很好地工作

jaql4c8m

jaql4c8m4#

下面的代码适合我。

struct ContentView: View {
    
    @State private var text = ""
    @State private var words: [String] = []
    
    
    var body: some View {
        VStack {
            Form {
                TextField("Enter word", text: $text)
            }.onSubmit {
                words.append(text)
                text = ""
            }
            List(words, id: \.self) { word in
                Text(word)
            }
        }
    }
}

相关问题