我不知道为什么,但是更新代码仅在初始加载时触发。它不会在对该字段进行连续更改时触发。
import SwiftUI
struct ParentView: View {
@State var text: String = "initial"
var body: some View {
VStack {
ChildView(text: text)
Text(self.text)
}
}
}
struct ChildView: View {
@State var text: String
var body: some View {
MyTextField(text: $text).frame(width: 300, height: 40, alignment: .center)
}
}
struct MyTextField: UIViewRepresentable {
@Binding var text: String
func makeUIView(context: Context) -> UITextField {
let view = UITextField()
view.borderStyle = UITextField.BorderStyle.roundedRect
return view
}
func updateUIView(_ uiView: UITextField, context: Context) {
print("updateView did fire")
uiView.text = $text.wrappedValue
}
}