TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Simple Picker

Forums > SwiftUI

@Jonne  

I have a picker in a project that's not working, it doesn't let me pick an option. I made a simple version that resembles the problem. I expect it's something simple but I can't seem to find an answer.

@State private var newActivity: String = "Option 1"

    var body: some View {

        Picker("Activity", selection : $newActivity) {
            ForEach(1...2, id: \.self) { item in
                Text("Option \(item)")
            }
        }
    }

2      

@Jonne is having problems making a proper selection.

I have a picker in a project that's not working, it doesn't let me pick an option.

Try thinking of a picker as having two parts. One is that which your user sees. The other is the value that's stored when selected. They may, or may not, be the same!

struct OptionPickerView: View {
    @State private var mySelection: String = "Option 1"
    var body: some View {
        // Add a text view for debugging!
        Text("Selection: \(mySelection)")

        // What are you putting into $mySelection?
        Picker("Activity", selection : $mySelection) {
            ForEach(1...4, id: \.self) { item in
                // The text is just what you see on screen
                Text("Option \(item)")
                // The tag is what you're putting into $mySelection
                    .tag("Option \(item)")
                    // Try changing the tag to this. What is the selection?
                    // .tag("\(item)")
            }
        }
    }
}

Keep coding!

3      

Here's anther picker example that doesn't use the tag....

struct TransportationView: View {

    // CaseIterable creates an array of all cases....
    // Also you're telling Swift the value of each case is a string
    enum TransportationOptions: String, CaseIterable {
        case flying, walking, rollerskating, swimming
    }

    // Note!  mySelection is NOT a string. It's an enum case.
    @State private var mySelection = TransportationOptions.walking
    var body: some View {
        // Add a text view for debugging!
        // Note! Show the rawValue of the enum case
        Text("You will be \(mySelection.rawValue)")
            .font(.largeTitle)

        // What are you putting into $mySelection?
        Picker("Activity", selection : $mySelection) {
            // Note!  option is an enum case. It's not a string!
            ForEach(TransportationOptions.allCases, id: \.self) { option in
                Text("\(option.rawValue)")
            }
        }
    }
}

Keep coding!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.