dyl*_*ley 5 textfield ios swiftui
我有一个简单的TextField可以像这样绑定到状态“位置”,
TextField("Search Location", text: $location)
Run Code Online (Sandbox Code Playgroud)
我想在每次该字段更改时调用一个函数,如下所示:
TextField("Search Location", text: $location) {
self.autocomplete(location)
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用。我知道有一些回调,onEditingChanged-但是,仅当该字段聚焦时,才似乎触发该回调。
如何在每次更新字段时调用此函数?
Moj*_*ini 50
从 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于较旧的 iOS 和其他 SwiftUI 1.0 平台,您可以使用onReceive:
.onReceive(location.publisher) {
print($0)
}
Run Code Online (Sandbox Code Playgroud)
请注意,它返回更改而不是整个值。如果您需要与 相同的行为onChange,您可以使用组合并遵循@pawello2222 提供的答案。
sup*_*cio 30
如果您需要使用 a 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)
}
}
Run Code Online (Sandbox Code Playgroud)
paw*_*222 16
使用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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用自定义闭包创建绑定,如下所示:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现最有用的是 TextField 有一个名为 onEditingChanged 的属性,该属性在编辑开始和编辑完成时调用。
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)
Run Code Online (Sandbox Code Playgroud)
虽然其他答案可能有用,但这个答案对我有用,我需要听文本变化并对其做出反应。
第一步创建一个扩展函数。
extension Binding {
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { newValue in
self.wrappedValue = newValue
handler(newValue)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
现在对TextField 中的绑定调用更改,如下所示。
TextField("hint", text: $text.onChange({ (value) in
//do something here
}))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
641 次 |
| 最近记录: |