-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextField or UITextView Representable not working #3
Comments
Hi @bluelobe I guess I got your problem, but I'll show you an example, so that you can tell me if it's actually the problem you are experiencing. The problem is due to SwiftUI resetting all the
The result is: SwiftUI resets the
This time the result is: Let me know if I answered your question and feel free to post some minimum viable examples in order for me to directly check them and help you debugging. |
Hi.
Thanks for answering. I discovered the cause to be the native Picker’s response method interfering with the TextView UIRepresentable. I had to give up using the Picker and just used 2 Buttons.
Vip Malixi
"Commit random acts of kindness."– The Dalai Lama
On Saturday, February 8, 2020, 10:24:45 PM GMT+8, Matteo <notifications@github.com> wrote:
Hi @bluelobe I guess I got your problem, but I'll show you an example, so that you can tell me if it's actually the problem you are experiencing. The problem is due to SwiftUI resetting all the @State properties when a view is removed from the hierarchy. It's not a SwiftUI bug, it's just a SwiftUI behaviour. Take a look at this example (you can copy paste it and try yourself):
import SwiftUI
import NavigationStack
struct ContentView : View {
var body: some View {
NavigationStackView {
View1()
}
}
}
struct View1: View {
@State private var text = ""
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
VStack {
PushView(destination: View2()) {
Text("PUSH")
}
TextField("Type something...", text: $text)
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
PopView {
Text("POP")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The result is:
SwiftUI resets the @State of the View1 when it's removed from the view hierarchy. Fortunately there's a simple workaround: you must put all the things that are @State and that must survive the transitions in an @observableobject class (i.e. in a ViewModel) this way:
import SwiftUI
import NavigationStack
struct ContentView : View {
var body: some View {
NavigationStackView {
View1()
}
}
}
class View1ViewModel: ObservableObject {
@published var text = ""
}
struct View1: View {
@ObservedObject var view1ViewModel = View1ViewModel()
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
VStack {
PushView(destination: View2()) {
Text("PUSH")
}
TextField("Type something...", text: $view1ViewModel.text)
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
PopView {
Text("POP")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This time the result is:
Let me know if I answered your question and feel free to post some minimum viable examples in order for me to directly check them and help you debugging.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
If you have any TextField or a UITextView Representable object in any view that is inside NavigationStack, user is prevented from typing anything. The text just disappears.
e.g.
TextField("test", text: $workingText)
The text was updated successfully, but these errors were encountered: