Update TextField text binding in alert

Viewed 11

I'm trying to update a TextField's text in an alert but it doesn't work. As you can see in the example the TextField outside the alert is updated but not inside the alert. Is this a known bug or is there a workaround for it?

MRE:

struct Test: View {
    
    @State private var text = "Hello"
    @State private var showAlert = true
    var body: some View {
        TextField("", text: $text)
            .alert("Test", isPresented: $showAlert, actions: {
                TextField("", text: $text)

                Button("OK") {
                }
                        
                Button("Cancel", role: .cancel) {
                }
            })
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                    text = "World"
                }
            }
    }
}
0 Answers
Related