I have a problem when I load the NormDetailView
of my app. It loads the default norm always instead of the selected norm by user, but after loading default one, it loads the norm selected by user.
Before that I was using NavigationLink
and it was working perfectly how I wanted. I'm adding the code that I was using without Sheet
for example:
VStack {
List {
ForEach(normsSearch) {norm in
Section(header: Text(norm.title)) {
ForEach(norm.elements) { element in
NavigationLink(destination: NormDetailView(chosenNormElement: element)) {
Text(element.name)
}
}
}
}
}
I know that it's always loading the default norm for the first time because I checked it with using breakpoint
. I couldn't find any solutions and I want your help. I'm putting the codes that I'm using now below for your inspection. Thanks for any help in advance
//The bool that I used to control the NormDetailsView
@State var showingSheet = false
//I need to create this one to use at .sheet part
@State var chosenElement: NormElements = en103052
var body: some View {
NavigationView {
VStack {
List {
ForEach(normsSearch) {norm in
Section(header: Text(norm.title)) {
ForEach(norm.elements) { element in
Button(element.name) {
//I added this to pass "element" value to "chosenElement" that I created above
chosenElement = element
showingSheet.toggle()
}
}
}
}
}.sheet(isPresented: $showingSheet, content: {
//I need to attach a chosenNormElement because I'm getting the file name from the button that clicked by user to open PDF file
NormDetailsView(chosenNormElement: chosenElement)
})