Home > Blockchain >  Supply any string value to swiftUI form in different for textfield
Supply any string value to swiftUI form in different for textfield

Time:11-01

I have two view file. I have textfield. I want to supply string value to the textfield from another view

File 1 :- Place where form is created

struct ContentView: View {
    
    @State var subjectLine: String = ""
    
    var body: some View {
        
        form {
            Section(header: Text(NSLocalizedString("subjectLine", comment: ""))) {
                TextField("SubjectLine", text: $subjectLine
                }
            }
        }
    }

File 2 :- Place where I want to provide value to the string and that will show in the textfield UI

struct CalenderView: View {
    
    @Binding var subjectLine: String
    
    var body : some View {
        Button(action: {
            subjectLine = "Assigned default value"
            
            
        }, label: {
            Text("Fill textfield")
        }
               }
               })
    }
    
}

This is not working. Any other way we can supply value to the textfield in other view file.

CodePudding user response:

As i can understand you have binding in CalenderView that means you want to navigate there when you navigate update there.

struct ContentView: View {
    @State private var subjectLine: String = ""
    @State private var showingSheet: Bool = false
    
    var body: some View {

        NavigationView {
            Form {
                Section(header: Text(NSLocalizedString("subjectLine", comment: ""))) {
                    TextField("SubjectLine", text: $subjectLine)
                }
            }
            .navigationBarItems(trailing: nextButton)
            .sheet(isPresented: $showingSheet) {
                CalenderView(subjectLine: $subjectLine)
            }
        }
    }
    var nextButton: some View {
        Button("Next") {
            showingSheet.toggle()
        }
     }
}

CalendarView

struct CalenderView: View {
    
    @Binding var subjectLine: String
    @Environment(\.dismiss) private var dismiss
    
    var body: some View {
        Button {
            subjectLine = "Assigned default value"
            dismiss()
        } label: {
            Text("Fill textfield")
        }
    }
}

enter image description here

  • Related