Homd
Explore posts from serversTSDThe Swift Den
•Created by Homd on 9/17/2023 in #swift-development
OnChange() doesn't trigger inside picker navigation view
For some reason the on change that I put on textfield only triggered after I close the selection view.
Picker("Game", selection: $selectedGame) {
Text("No Game Selected").tag(0 as Int?)
Section{
TextField("Search Here", text: $gv.searchTerm).onChange(of: gv.searchTerm) { newValue in
print("On Change Triggered")
searchTimer?.invalidate()
searchTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
Task{
try await gv.fetchGames(searchQuery: newValue)
}
}
}
}
ForEach(gv.games, id: \.self) { game in
Text(game.name).tag(game.id as Int?)
}
}
.pickerStyle(.navigationLink)
}
Picker("Game", selection: $selectedGame) {
Text("No Game Selected").tag(0 as Int?)
Section{
TextField("Search Here", text: $gv.searchTerm).onChange(of: gv.searchTerm) { newValue in
print("On Change Triggered")
searchTimer?.invalidate()
searchTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
Task{
try await gv.fetchGames(searchQuery: newValue)
}
}
}
}
ForEach(gv.games, id: \.self) { game in
Text(game.name).tag(game.id as Int?)
}
}
.pickerStyle(.navigationLink)
}
5 replies