我在玩 Swift UI。我创建了一个包含所有我想要的元素的表格视图(一个列表)。按键盘上的 Enter 键时,TextField 更改也有效。很高兴能如此快速地构建一个表格视图;)。
但现在我想跟踪文本字段上的每一个变化。每次用户输入另一个文本时,我都想发送一个触发搜索的请求。
这怎么能在我的代码中完成?我也在这里阅读了这个文档https://developer.apple.com/documentation/combine/receiving_and_handling_events_with_combine
我现在不知道如何在我的结构中使用它。
此处建议的解决方案TextField changes in SwiftUI不起作用(不再)
我会很感激你能与我分享的每一个提示和每一个想法。我正在寻找解决方案的时间:(。
我的代码如下所示:
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()
} …Run Code Online (Sandbox Code Playgroud) I've implemented a List with a search bar in SwiftUI. Now I want to implement paging for this list. When the user scrolls to the bottom of the list, new elements should be loaded. My problem is, how can I detect that the user scrolled to the end? When this happens I want to load new elements, append them and show them to the user.
My code looks like this:
import Foundation
import SwiftUI
struct MyList: View {
@EnvironmentObject var …Run Code Online (Sandbox Code Playgroud)