Skip to content
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

Closed
bluelobe opened this issue Feb 7, 2020 · 2 comments
Closed

TextField or UITextView Representable not working #3

bluelobe opened this issue Feb 7, 2020 · 2 comments

Comments

@bluelobe
Copy link

bluelobe commented Feb 7, 2020

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)

@matteopuc
Copy link
Owner

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:

Feb-08-2020 15-10-43

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:

Feb-08-2020 15-20-09

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.

@bluelobe
Copy link
Author

bluelobe commented Feb 9, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants