我有一个简单的文本字段,它绑定到状态"位置",如下所示,
TextField("Search Location", text: $location)
我想每次这个字段更改时调用一个函数,如下所示:
TextField("Search Location", text: $location) {
self.autocomplete(location)
}
但是,这不起作用。我知道有回调,在编辑更改 - 但这似乎仅在字段聚焦时触发。
如何让此函数在每次更新字段时调用?
可以使用自定义闭包创建绑定,如下所示:
struct ContentView: View {
@State var location: String = ""
var body: some View {
let binding = Binding<String>(get: {
self.location
}, set: {
self.location = $0
// do whatever you want here
})
return VStack {
Text("Current location: (location)")
TextField("Search Location", text: binding)
}
}
}
SwiftUI 2.0
从iOS 14,macOS 11或任何其他包含SwiftUI 2.0的操作系统中,有一个名为.onChange
的新修饰符,可以检测给定state
的任何更改:
struct ContentView: View {
@State var location: String = ""
var body: some View {
TextField("Your Location", text: $location)
.onChange(of: location) {
print($0) // You can do anything due to the change here.
// self.autocomplete($0) // like this
}
}
}
<小时 />SwiftUI 1.0
对于较旧的 iOS 和其他 SwiftUI 1.0 平台,您可以使用onReceive
:
.onReceive(location.publisher) {
print($0)
}
**请注意,** 它返回 **更改** 而不是整个值。如果您需要与"onChange"相同的行为,则可以使用**组合**并按照@pawello2222提供的答案进行操作。如果您需要使用ViewModel
,另一种解决方案可能是:
import SwiftUI
import Combine
class ViewModel: ObservableObject {
@Published var location = "" {
didSet {
print("set")
//do whatever you want
}
}
}
struct ContentView: View {
@ObservedObject var viewModel = ViewModel()
var body: some View {
TextField("Search Location", text: $viewModel.location)
}
}
iOS 13+
使用onReceive
:
import Combine
import SwiftUI
struct ContentView: View {
@State var location: String = ""
var body: some View {
TextField("Search Location", text: $location)
.onReceive(Just(location)) { location in
// print(location)
}
}
}
虽然其他答案可能有效,但这个答案对我有用,我需要听文本变化并对此做出反应。
第一步创建一个扩展函数。
extension Binding {
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { newValue in
self.wrappedValue = newValue
handler(newValue)
}
)
}
}
现在调用TextField中绑定的更改,如下所示。
TextField("hint", text: $text.onChange({ (value) in
//do something here
}))
来源 : HackingWithSwift
我发现最有用的是 TextField 有一个名为 onEditChanged 的属性,该属性在编辑开始和编辑完成时调用。
TextField("Enter song title", text: self.$userData.songs[self.songIndex].name, onEditingChanged: { (changed) in
if changed {
print("text edit has begun")
} else {
print("committed the change")
saveSongs(self.userData.songs)
}
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.font(.largeTitle)