How can I pass value from TextField to Alert?

Hello,

I have a view has 4 textfields and a button. I want to show 4 values from textfields on popup messagebox. so I wrote a following codes.

 @State private var lender = ""
    @State private var amount = ""
    @State private var rate = ""
    @State private var duration = ""

Button(action: {showingAlert = true},
                    label: {
                    Text("Inputs")
                        .padding()
                        .background(RoundedRectangle(cornerRadius: 10).strokeBorder())
                }).padding(10)
                    .accentColor(.green)
                    .alert("Inputs", isPresented: $showingAlert){
                        Text("Lender: \(lender)").font(.title3)
                        Text("Amount:\(amount)").font(.title3)
                        Text("Rate:\(rate)")
                        Text("Duration:\(duration)")
                        Button("OK", role: .cancel){}
                    }

When I run this code, Alert popped up but there's no values. If someone point my mistakes, I'd very appreciated.

thanks,

c00012

Answered by DTS Engineer in 791111022

@c00012 From the code snippet you share, it seems like you're missing the TextFields. Could you try this instead:

struct ContentView: View {
    @State private var lender = ""
    @State private var amount = ""
    @State private var rate = ""
    @State private var duration = ""
    @State private var showingAlert = false

    var body: some View {
        VStack {
            TextField("Lender", text: $lender)
                .padding()
                .textFieldStyle(.roundedBorder)
            
            TextField("Amount", text: $amount)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            TextField("Rate", text: $rate)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            TextField("Duration", text: $duration)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            Button(action: {
                showingAlert = true
            }) {
                Text("Inputs")
                    .padding()
                    .textFieldStyle(.roundedBorder)
            }
            .padding(10)
            .accentColor(.green)
            .alert("Inputs", isPresented: $showingAlert) {
                VStack(alignment: .leading) {
                    Text("Lender: \(lender)").font(.title3)
                    Text("Amount: \(amount)").font(.title3)
                    Text("Rate: \(rate)")
                    Text("Duration: \(duration)")
                }
            }
        }
        .padding()
    }
}

@c00012 From the code snippet you share, it seems like you're missing the TextFields. Could you try this instead:

struct ContentView: View {
    @State private var lender = ""
    @State private var amount = ""
    @State private var rate = ""
    @State private var duration = ""
    @State private var showingAlert = false

    var body: some View {
        VStack {
            TextField("Lender", text: $lender)
                .padding()
                .textFieldStyle(.roundedBorder)
            
            TextField("Amount", text: $amount)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            TextField("Rate", text: $rate)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            TextField("Duration", text: $duration)
                .padding()
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
            
            Button(action: {
                showingAlert = true
            }) {
                Text("Inputs")
                    .padding()
                    .textFieldStyle(.roundedBorder)
            }
            .padding(10)
            .accentColor(.green)
            .alert("Inputs", isPresented: $showingAlert) {
                VStack(alignment: .leading) {
                    Text("Lender: \(lender)").font(.title3)
                    Text("Amount: \(amount)").font(.title3)
                    Text("Rate: \(rate)")
                    Text("Duration: \(duration)")
                }
            }
        }
        .padding()
    }
}

How can I pass value from TextField to Alert?
 
 
Q