Home > AI > IOS > SwiftUI >

(View)Picker

Example 1:

enum Flavor: String, CaseIterable, Identifiable {
    case chocolate
    case vanilla
    case strawberry

    var id: String { self.rawValue }
}



struct ContentView: View {
    @State private var selectedFlavor = Flavor.chocolate
    
    var body: some View {
        VStack {
            Picker("Flavor", selection: $selectedFlavor) {
                Text("Chocolate").tag(Flavor.chocolate)
                Text("Vanilla").tag(Flavor.vanilla)
                Text("Strawberry").tag(Flavor.strawberry)
            }
            Text("Selected flavor: \(selectedFlavor.rawValue)")
        }
    }
}

Example 2:

Use ForEach to implicitly embed tag. You need id in the ForEach to have effects.

enum Flavor: String, CaseIterable, Identifiable {
    case chocolate
    case vanilla
    case strawberry

    var id: String { self.rawValue }
}

struct ContentView: View {
    @State private var selectedFlavor = Flavor.chocolate
    
    var body: some View {
        VStack {
            Picker("Flavor", selection: $selectedFlavor) {
                ForEach(Flavor.allCases, id:\.self) { flavor in
                    Text(flavor.rawValue.capitalized)
                }
            }
            Text("Chosen flavor is \(selectedFlavor.rawValue)")
        }
                   
    }
}

Example 3:

enum Flavor: String, CaseIterable, Identifiable {
    case chocolate
    case vanilla
    case strawberry

    var id: String { self.rawValue }
}

enum Topping: String, CaseIterable, Identifiable {
    case nuts
    case cookies
    case blueberries

    var id: String { self.rawValue }
}

extension Flavor {
    var suggestedTopping: Topping {
        switch self {
        case .chocolate: return .nuts
        case .vanilla: return .cookies
        case .strawberry: return .blueberries
        }
    }
}

struct ContentView: View {
    @State private var selectedFlavor = Flavor.chocolate
    @State var suggestedTopping: Topping = .cookies
    
    var body: some View {
        VStack {
            

            Picker("Suggest a topping for:", selection: $suggestedTopping) {
                ForEach(Flavor.allCases) { flavor in
                    Text(flavor.rawValue.capitalized)
                        .tag(flavor.suggestedTopping)
                }
            }
            Text("suggestedTopping: \(suggestedTopping.rawValue)")
        }
    }
}

Example 4: SegmentedPickerStyle

enum Flavor: String, CaseIterable, Identifiable {
    case chocolate
    case vanilla
    case strawberry

    var id: String { self.rawValue }
}

struct ContentView: View {
    @State private var selectedFlavor = Flavor.chocolate
    
    var body: some View {
        VStack {
            Picker("Flavor", selection: $selectedFlavor) {
                ForEach(Flavor.allCases, id:\.self) { flavor in
                    Text(flavor.rawValue.capitalized)
                }
            }
            Text("Chosen flavor is \(selectedFlavor.rawValue)")
        }.pickerStyle(SegmentedPickerStyle())
                   
    }
}

Leave a Reply