Hi I'm new to Swiftui.
I want to show a sheet and pass a var to it.
In the case below when I tap on a list view it will popup then disappear. But subsequent taps will work as intended.
Main view:
struct ContentView: View {
@State private var selectedMonth = 1
@State private var selectedYear = "2024"
let months = [1,2,3,4,5,6,7,8,9,10,11,12]
@State private var isShowing = false
var body: some View {
List {
ForEach (months, id: \.self) { month in
HStack {
ViewList(month: month, year: selectedYear)
}
.onTapGesture {
isShowing = true
selectedMonth = month
}
.sheet(isPresented: $isShowing){
PopupView(month: selectedMonth, year: selectedYear)
.presentationDetents([.large])
}
}
}
.listRowSpacing(2)
.listStyle(.grouped)
}
}
ViewList:
struct ViewList: View {
var month: Int
var year: String
@State private var selectedMonthText = "Jan"
var body: some View {
VStack (alignment: .leading) {
Text(selectedMonthText + " / " + year)
.font(.headline)
}
.onAppear {
switch month {
case 01:
selectedMonthText = "Jan"
case 02:
selectedMonthText = "Feb"
case 03:
selectedMonthText = "Mar"
case 04:
selectedMonthText = "Apr"
case 05:
selectedMonthText = "May"
case 06:
selectedMonthText = "Jun"
case 07:
selectedMonthText = "Jul"
case 08:
selectedMonthText = "Aug"
case 09:
selectedMonthText = "Sep"
case 10:
selectedMonthText = "Oct"
case 11:
selectedMonthText = "Nov"
case 12:
selectedMonthText = "Dec"
default:
selectedMonthText = "All"
}
}
}
}
Then my popup:
struct PopupView: View {
@Environment(\.dismiss) var dismiss
var month: Int
var year: String
@State private var selectedMonthText = "Jan"
@State private var imageText = "plus"
@State private var items = ["Cat", "Dog", "Bird", "Snake"]
var body: some View {
Button("Dismiss"){
dismiss()
}
Text(selectedMonthText + " / " + year)
List {
ForEach(items, id: \.self) {item in
HStack {
Text(item)
Text("Fed on: ")
Text(selectedMonthText)
}
}
}
.listRowSpacing(0)
.listStyle(.inset)
.onAppear {
switch month {
case 01:
selectedMonthText = "Jan"
case 02:
selectedMonthText = "Feb"
case 03:
selectedMonthText = "Mar"
case 04:
selectedMonthText = "Apr"
case 05:
selectedMonthText = "May"
case 06:
selectedMonthText = "Jun"
case 07:
selectedMonthText = "Jul"
case 08:
selectedMonthText = "Aug"
case 09:
selectedMonthText = "Sep"
case 10:
selectedMonthText = "Oct"
case 11:
selectedMonthText = "Nov"
case 12:
selectedMonthText = "Dec"
default:
selectedMonthText = "All"
}
}
}
}