article

Saturday, May 8, 2021

SwiftUI Form Picker How to get value from for loop

SwiftUI Form Picker How to get value from for loop
 
//
//  ContentView.swift
//  Testapp
//
//  Created by Cairocoders
//  

import SwiftUI

struct ContentView: View {
    
    @State private var location = ""

    var body: some View {
        NavigationView {
            Form {
                Picker(selection: $location,
                   label: Text("Location")) {
                    ForEach(Location.allLocations, id: \.self) { location in
                        Text(location).tag(location)
                    }
                }
            }
        }
    }
}

struct Location {
    static let allLocations = [
        "New York",
        "London",
        "Tokyo",
        "Berlin",
        "Olongapo",
        "Paris"
    ]
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

Related Post