Removing multiple sheets after swiping down the last one swiftui

Viewed 40

I currently have two sheets in a row and I want them to be dismissed to the view that called the first sheet once the last sheet is dismissed by the user. I am open to not pulling up views as sheets its just the way I learned how to easily pull up new views.

BookView is what I want to be returned after the PickDefinition sheet view has been dismissed.

BookView pulls up AddWord as a sheet.

AddWord is pulled up as a sheet and then in it PickDefinition is pulled up as a sheet.

After PickDefinition is dismissed I would like for it to go back to the BookView

struct BookView: View {
    @ObservedObject var book: Book
    @State var addingWord = false
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            List(Array(zip(book.words, book.definitions)), id: \.self.0) { (word, definition) in
                Text("\(word) - \(definition)")
            }
        }
        .onAppear(perform: {
            DB_Manager().openBook(name: book.name, book: self.book)
        })
        .navigationBarTitle("\(book.name)")
        .navigationBarItems(trailing: Button(action: {
            self.addingWord = true
        }) {
            Image(systemName: "plus")
        }
        ).sheet(isPresented: $addingWord) {
            AddWord(book: self.book)
        }
    }
}


struct AddWord: View {
    @ObservedObject var book: Book
    @StateObject var currentArray = SaveArray()
    @State var addingDefinition = false
    @State var word = ""
    @State var definition = ""
    @State var allDefinitions: [String] = []

    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            Form {
                TextField("Word: ", text: $word)
            }
            .navigationBarTitle("Add word")
            .navigationBarItems(trailing: Button("Add") {
                if self.word != "" {
                    book.words.append(self.word)

                    getDef(self.word, book, currentArray)
                    addingDefinition = true
                }
            }).sheet(isPresented: $addingDefinition) {
                
                PickDefinition(definitions: currentArray, book: book, word: self.word)
            }

        }
    }



struct PickDefinition: View {
    @ObservedObject var definitions: SaveArray
    var book: Book
    var word: String
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(0 ..< definitions.currentArray.count) { index in
                        Button("\(self.definitions.currentArray[index])", action: {
                            print("hello")
                            DB_Manager().addWords(name: self.book.name, word: self.word, definition: self.definitions.currentArray[index])
                            book.definitions.append(self.definitions.currentArray[index])
                            self.presentationMode.wrappedValue.dismiss()

                        })
                    }
                }
            }
            .navigationTitle("Choose")
            
        }
    }

}
1 Answers

You need to pass addingWord into the AddWord view, and then into the PickDefinition view, using Binding. When PickDefinition disappears, set the passed property to false to hide the AddWord sheet.

struct BookView: View {
  var body: some View {
    // ...
    .sheet(isPresented: $addingWord) {
      AddWord(book: self.book, presentAddingWord: $addingWord)
    }
  }
}

struct AddWord: View {
  @Binding var presentAddingWord: Bool
  // ...
  var body: some View {
    // ...
    .sheet(isPresented: $addingDefinition) {
      PickDefinition(/* ... */, presentAddingWord: $presentAddingWord)
    }
  }
}

struct PickDefinition: View {
  @Binding var presentAddingWord: Bool
  // ...
  var body: some View {
    // ...
    .onDisappear {
      presentAddingWord = false
    }
  }
}
Related