Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PetreVane/df39253731b9892da2a5e84d90de70ea to your computer and use it in GitHub Desktop.
Save PetreVane/df39253731b9892da2a5e84d90de70ea to your computer and use it in GitHub Desktop.
Example of a Form, embedded in a NavigationView, which has a Section and Picker. When Selecting an item, a new window slides in allowing you to select something. The selected value is passed back to the original screen.
import SwiftUI
struct ContentView: View {
@State private var pickerSelectedIndex = 0
var treats = Treat.demoTreats
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $pickerSelectedIndex, label: Text("Treats")) {
ForEach(0 ..< self.treats.count) {
Text(self.treats[$0].name).tag($0)
}
}
}
}.navigationBarTitle(Text("Treats for your cat"))
}
}
}
struct Treat: Identifiable {
var id = UUID()
var name: String
var imageName: String
var description: String
}
extension Treat {
static let demoTreats = [
Treat(name: "Fish Cakes", imageName: "FlyingFish", description: "Lots of fish, lots of cakes!"),
Treat(name: "Mice Cream", imageName: "MiceCream", description: "Every kitty's favorite flavored Ice Cream"),
Treat(name: "Croissant", imageName: "Croissant", description: "Seems like cats wouldn't like them, but they do!"),
Treat(name: "Pancakes", imageName: "PanCakes", description: "They're cakes, but in the shape of a pan"),
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment