[英]Change characters typed on TextEditor in SwiftUI
I'm trying to capture the text typed in a TextEditor, store it in a variable and update what is displayed on the TextEditor itself.我正在尝试捕获在 TextEditor 中键入的文本,将其存储在变量中并更新 TextEditor 本身上显示的内容。 I'm using the
.onChange()
to trigger and capture what is being typed, store it in a variable, and change the current text being displayed.我正在使用
.onChange()
来触发和捕获正在输入的内容,将其存储在变量中,并更改当前显示的文本。 But the problem is that, .onChange()
is being called twice: First, when i type the text inside the TextEditor;但问题是,
.onChange()
被调用了两次:首先,当我在 TextEditor 中键入文本时; and Second, when i try to set a different character for the view itself.其次,当我尝试为视图本身设置不同的字符时。
struct ChatView: View {
@State var chatTextInput: String = ""
@State var oldChatValue: String = ""
@State var newChatValue: String = ""
var body: some View {
VStack {
TextEditor(text: $chatTextInput)
.onChange(of: chatTextInput) { newValue in
oldChatValue += "a"
newChatValue += newValue.last.map{String($0)}!
chatTextInput = oldChatValue
}
}
}
}
For exemple, if i type qwerty , newChatValue
is printed as Qawaearataya and chatTextInput
receives aaaaaaaaaaaa例如,如果我键入qwerty ,
newChatValue
将打印为Qawaearataya并且chatTextInput
接收aaaaaaaaaaaaa
Any clues on how to prevent the .onChange to being triggered the second time, when i set the new character for the TextEditor text?当我为 TextEditor 文本设置新字符时,有关如何防止 .onChange 被第二次触发的任何线索?
Thanks a lot!非常感谢!
Your issue is simply that .onChange(of:)
isn't a responder to the TextEditor
, it is a responder for the variable, in this case chatTextInput
.您的问题只是
.onChange(of:)
不是TextEditor
的响应者,它是变量的响应者,在本例中chatTextInput
。 Since chatTextInput
is bound to the TextEditor
, when you type a letter, chatTextInput
is updated, and .onChange(of:)
runs.由于
chatTextInput
绑定到TextEditor
,当您键入一个字母时, chatTextInput
会更新,并且.onChange(of:)
运行。 However, as part of .onChange(of:)
running, you change chatTextInput
which calls .onChange(of:) again. You would have an infinite loop, except that
但是,作为
.onChange(of:)
运行的一部分,您更改了再次调用chatTextInput
.onChange(of:) again. You would have an infinite loop, except that
.onChange(of:) again. You would have an infinite loop, except that
.onChange(of:)` ends at this point. .onChange(of:) again. You would have an infinite loop, except that
.onChange(of:)` 在这一点上结束。 If anyone can explain that, I would love to hear it.如果有人能解释一下,我很想听听。
The fix, however is to simply put a flag in .onChange(of:)
to only set the variables every other time like this:然而,解决方法是简单地在
.onChange(of:)
中放置一个标志,以便每隔一次设置变量,如下所示:
struct ChatView: View {
@State var chatTextInput: String = ""
@State var oldChatValue: String = ""
@State var newChatValue: String = ""
@State var textEntryFlag = true
var body: some View {
VStack {
TextEditor(text: $chatTextInput)
.onChange(of: chatTextInput) { newValue in
if textEntryFlag {
oldChatValue += "a"
newChatValue += newValue.last.map{String($0)}!
chatTextInput = oldChatValue
}
textEntryFlag.toggle()
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.