Post

Replies

Boosts

Views

Activity

Reply to Updating @State Variable Depending ForEach Row Selection
It seems that I have achieved my objective with the following. But I'm not sure if it's an efficient approach. import SwiftUI struct ContentView: View { @State var numbers = [2021, 9, 30] var body: some View { let firstLocalYear = 2021 let firstLocalMonth = 9 let firstLocalDay = 24 let firstLastDay = 30 NavigationView { List { Section(header: Text("Current month")) { ForEach(firstLocalDay ..< firstLastDay) { i in HStack { Text("\(firstLocalMonth)-\(i + 1)") Spacer() Button("Select") { self.numbers = [firstLocalYear, firstLocalMonth, i + 1] } NavigationLink( destination: TimeView(numbers: $numbers), label: { Text("") }) } } } } } } } struct TimeView: View { @Binding var numbers: [Int] var body: some View { HStack { Text(String(numbers[0])) Text(String(numbers[1])) Text(String(numbers[2])) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21