Home > Software engineering >  displaying alert on picker select SwiftUI
displaying alert on picker select SwiftUI

Time:07-15

i want to display an alert box when a user selects something in a form picker the user then have to confirm their choice before the value changes. right now my code looks like this(just from some tutorial):

            NavigationView {
            Form {
                Section {
                    Picker("Strength", selection: $selectedStrength) {
                        ForEach(strengths, id: \.self) {
                            Text($0)
                        
                            
                        }
                    }
                }
            }
        }

I have tried using onChange() but ideally the check should be before the value changes.

CodePudding user response:

If you want to ask the user to confirm before the selection changes you would need to implement a custom binding with a second var. With this you would be able to cancel the selection if necessary.

struct Test: View{
    @State private var selectedStrength: String = ""
    @State private var askForStrength: String = ""
    @State private var ask: Bool = false
    let strengths = ["1","2","3"]
    var body: some View{
        NavigationView {
            Form {
                Section {
                    Picker("Strength", selection: Binding(get: {selectedStrength}, set: {
                        //assign the selection to the temp var
                        askForStrength = $0
                        // show the Alert
                        ask = true
                    })) {
                        ForEach(strengths, id: \.self) {
                            Text($0)
                        }
                    }
                }
            }
        }.alert(isPresented: $ask) {
            // Here ask the user if selection is correct and apply the temp var to the selection
            Alert(title: Text("select?"), message: Text("Do you want to select \(askForStrength)"), primaryButton: .default(Text("select"), action: {selectedStrength = askForStrength}), secondaryButton: .cancel())
        }
    }
}

CodePudding user response:

You can do it this way; having an alert after the value is clicked and a temp variable for storing pre-selected data. Code is below the image: enter image description here

import SwiftUI

struct ContentView: View {

let animals = ["dog", "cat", "pig"]
@State var selected = ""
@State var finalResult = ""
@State var alert = false

var body: some View {
    NavigationView {
        Form {
            Section {
                Picker("Animals", selection: $selected) {
                    ForEach(animals, id: \.self) {
                        Text($0)
                    }
                }
                .onChange(of: selected) { _ in
                    alert.toggle()
                }
                Text("You have confirmed to select this: \(finalResult)")
            }
            .alert("Confirm selection?", isPresented: $alert) {
                Button("Confirm", role: .destructive) {
                    finalResult = selected
                }
            }
        }
    }
  }
}
  • Related