Skip to content

Instantly share code, notes, and snippets.

@ravisharmaa
Created October 21, 2021 00:55
Show Gist options
  • Select an option

  • Save ravisharmaa/346669dc4da1fd4a5342e272f692ab22 to your computer and use it in GitHub Desktop.

Select an option

Save ravisharmaa/346669dc4da1fd4a5342e272f692ab22 to your computer and use it in GitHub Desktop.
import SwiftUI
// MARK: Multiple sheets cases.
enum SheetTest: Identifiable {
var id: UUID {
return UUID()
}
case sheet1(value: Int)
var value: Int {
switch self {
case .sheet1(let value): return value }
}
}
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.white.ignoresSafeArea()
VStack(spacing: 10) {
ForEach(0..<4) { _ in
NavigationLink {
DestinationView(bodyText: "Destination View").navigationBarHidden(true)
} label: {
Text("Multiple Navigation links in loop")
}
}
}.foregroundColor(.blue)
}
}
}
}
// MARK: Desitnation View with multiple sheets.
struct DestinationView: View {
let bodyText: String
@State private var sheetTest: SheetTest?
@Environment (\.dismiss) var dismiss
var body: some View {
ZStack {
Color.white.ignoresSafeArea()
VStack {
HStack {
Image(systemName: "xmark")
.onTapGesture {
dismiss()
}
Spacer()
Text(bodyText)
Spacer()
}
.padding()
ScrollView {
VStack {
ForEach(0..<3) { index in
Button {
// selection of sheet.
sheetTest = .sheet1(value: index)
} label: { Text(bodyText) }
.tag(index).frame(width: UIScreen.main.bounds.width - 200, height: 50).background(Color.blue).foregroundColor(.white).padding()
}
}
}
}
.sheet(item: $sheetTest) {
sheetTest = nil
} content: { sheetTest in
Text(sheetTest.value.description)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment