You need to move the sheet modifier outside of the ForEach, as it's currently creating a sheet for every item bound to the same isPresented variable. When you set isPresented to true, you are essentially showing each item's sheet, but you only see the last one.
You can instead use the sheet(item:onDismiss:content:) modifier.
Something like this will work:
// the item parameter requires the data to be Identifiable
// you can make a custom struct to hold this, or manually conform String to Identifiable
struct Item: Identifiable {
let id = UUID()
let name: String
}
// you can remove the isPresented variable
@State selectedItem: Item?
var body: some View {
...
ForEach(...) { item in
Button {
selectedItem = Item(name: item)
} label: {
...
}
}
// move outside loop so there is only one sheet that shows a view dependant on the selectedItem
.sheet(item: $selectedItem) { item in
CustomView(text: item)
}
...
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: