SwiftUI:为什么 onReceive 在 ObservableObject 内绑定字段时运行重复?
这是我的代码,当文本更改(如图像)时,“print(”run to onReceive (text)”)”运行两次。为什么?谢谢你!
import SwiftUI
class ContentViewViewModel : ObservableObject {
@Published var text = ""
}
struct ContentView: View {
@StateObject private var viewModel = ContentViewViewModel()
var body: some View {
ZStack {
TextField("pla", text: $viewModel.text)
.padding()
}
.onReceive(viewModel.$text) { text in
print("run to onReceive \(text)")
}
}
}
This is my code and "print("run to onReceive (text)")" run twice when text change (like a image). Why? and thank you!
import SwiftUI
class ContentViewViewModel : ObservableObject {
@Published var text = ""
}
struct ContentView: View {
@StateObject private var viewModel = ContentViewViewModel()
var body: some View {
ZStack {
TextField("pla", text: $viewModel.text)
.padding()
}
.onReceive(viewModel.$text) { text in
print("run to onReceive \(text)")
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为视图会随着 ViewModel 中的
@Published
属性的更改而自动更新,并且.onReceive
修饰符会由于创建的 2 路绑定而再次更新视图viewModel.$text
导致视图每次更新两次。如果您想在文本更改时打印文本,可以使用
.onChange
修饰符。SwiftUI 中的 onChanged
I think it's because the view is automatically updated as your
@Published
property in your ViewModel changes and the.onReceive
modifier updates the view yet again due to the 2 way binding created byviewModel.$text
resulting in the view being updated twice each time.If you want to print the text as it changes you can use the
.onChange
modifier instead.onChanged in SwiftUI
因为视图的
@StateObject
中有一个@Published
变量,所以该变量中的更改将自动更新视图。如果添加
.onReceive()
方法,您将:@Published
var.onReceive()< /code> 方法监听变化
只需完全删除
.onReceive()
即可工作:Because you have a
@Published
variable inside the@StateObject
of your view, the changes in that variable will automatically update the view.If you add the
.onReceive()
method, you will:@Published
var.onReceive()
method listens to the changeJust delete the
.onReceive()
completely and it will work: