How to pass data to .sheet(isPresented)
I have 10 names that I get from my JSON API, but when I click on the name, the .sheet(isPresented)
shows one name.
In all my 10 .sheet(isPresented) only shows one first name
struct Course: Hashable, Codable {
let name: String
}
class ViewModel: ObservableObject {
@Published var courses: [Course] = []
func fetch() {
guard let url = URL(string: "JSON URL") else {return}
let task = URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
guard let data = data, error == nil else {return}
do {
let courses = try JSONDecoder().decode([Course].self, from: data)
DispatchQueue.main.async {
self?.courses = courses
}
}
catch {
print(error)
}
}
task.resume()
}
}
struct List: View {
@StateObject var viewModel = ViewModel()
@State var show = false
let columns: [GridItem] = [
GridItem(.flexible(), spacing: nil, alignment: nil),
GridItem(.flexible(), spacing: nil, alignment: nil),
GridItem(.flexible(), spacing: nil, alignment: nil)
]
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
LazyVGrid(columns: columns) {
ForEach(viewModel.courses, id: \.self) { course in
VStack {
Text(course.name)
.foregroundColor(.white)
.lineLimit(1)
.padding(.bottom)
}
.frame(width: 90, height: 90)
.background(Color.gray)
.onTapGesture {
self.show = true
}.sheet(isPresented: self.$show, onDismiss: {
self.show = false
}, content: {
Text(course.name)
})
}
}
}
.onAppear {
viewModel.fetch()
}
}
}
My JSON API from which I get name data looks like this
[
{
"id": 1,
"name": "Alex"
},
{
"id": 2,
"name": "Sam"
},
{
"id": 3,
"name": "Den"
}
]
sorry, I can't write a link here If you do not understand the essence of my question, ask me for the necessary details.