观察 SwiftUI TextField 上的变化
Observe changes on SwiftUI TextField
我在玩 Swift UI。我创建了一个 table 视图(一个列表),其中包含我想要的所有元素。当按下键盘上的 enter 时,TextField 更改也起作用。很高兴能够如此快速地构建 table 视图 ;)。
但现在我想跟踪文本字段上的每个更改。每次用户输入另一个文本时,我都想发送一个触发搜索的请求。
如何在我的代码中完成此操作?我也在此处阅读了此文档 https://developer.apple.com/documentation/combine/receiving_and_handling_events_with_combine
我现在不知道如何在我的结构中使用它。
此处提出的解决方案 不再有效
如果您能与我分享每一个提示和每一个想法,我将不胜感激。我正在寻找解决方案的时间:(.
我的代码如下所示:
import Foundation
import SwiftUI
struct MyListView: View {
@ObservedObject var viewModel: MyViewModel
@State private var query = ""
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField(String.localizedString(forKey: "search_bar_hint"), text: self.$query) {
self.fetchListing()
}
ForEach(viewModel.myArray, id: \.id) { arrayObject in
MyRow(arrayObj: arrayObject)
}
}
.navigationBarTitle(navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
private func fetchListing() {
query.isEmpty ? viewModel.fetchRequest(for: nil) : viewModel.fetchRequest(for: query)
}
private func navigationBarTitle() -> String {
return query.isEmpty ? String.localizedString(forKey: "my_title") : query
}
}
简单的自定义 binding
是完成此任务的良好开端:
struct ContentView: View {
@State private var query = ""
var body: some View {
let binding = Binding<String>(
get: { self.query },
set: { self.query = [=10=]; self.textFieldChanged([=10=]) }
)
return NavigationView {
List {
TextField("Text", text: binding)
}
}
}
private func textFieldChanged(_ text: String) { print(text) }
}
它已经过测试并且可以正常工作
开始了
struct ContentView: View {
@State private var email = ""
var body: some View {
TextField("Email", text: $email)
.onChange(of: email) { text in
print(text)
}
}
}
我在玩 Swift UI。我创建了一个 table 视图(一个列表),其中包含我想要的所有元素。当按下键盘上的 enter 时,TextField 更改也起作用。很高兴能够如此快速地构建 table 视图 ;)。
但现在我想跟踪文本字段上的每个更改。每次用户输入另一个文本时,我都想发送一个触发搜索的请求。
如何在我的代码中完成此操作?我也在此处阅读了此文档 https://developer.apple.com/documentation/combine/receiving_and_handling_events_with_combine
我现在不知道如何在我的结构中使用它。
此处提出的解决方案
如果您能与我分享每一个提示和每一个想法,我将不胜感激。我正在寻找解决方案的时间:(.
我的代码如下所示:
import Foundation
import SwiftUI
struct MyListView: View {
@ObservedObject var viewModel: MyViewModel
@State private var query = ""
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField(String.localizedString(forKey: "search_bar_hint"), text: self.$query) {
self.fetchListing()
}
ForEach(viewModel.myArray, id: \.id) { arrayObject in
MyRow(arrayObj: arrayObject)
}
}
.navigationBarTitle(navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
private func fetchListing() {
query.isEmpty ? viewModel.fetchRequest(for: nil) : viewModel.fetchRequest(for: query)
}
private func navigationBarTitle() -> String {
return query.isEmpty ? String.localizedString(forKey: "my_title") : query
}
}
简单的自定义 binding
是完成此任务的良好开端:
struct ContentView: View {
@State private var query = ""
var body: some View {
let binding = Binding<String>(
get: { self.query },
set: { self.query = [=10=]; self.textFieldChanged([=10=]) }
)
return NavigationView {
List {
TextField("Text", text: binding)
}
}
}
private func textFieldChanged(_ text: String) { print(text) }
}
它已经过测试并且可以正常工作
开始了
struct ContentView: View {
@State private var email = ""
var body: some View {
TextField("Email", text: $email)
.onChange(of: email) { text in
print(text)
}
}
}