0

I am currently in a view controller for a specific bottle detail view and provide users with an action sheet that enables them to delete the product. Since they can delete this product, I need to navigate to the previous controller once it's successfully deleted. How can I do this in Swift? This will be done in the event of a successful API request.

struct WishlistView: View {
    @State private var showingSheet = false
    @State private var show_modal: Bool = false
    let wishlist: Wishlist

    var body: some View {
        Text("Hello, Marketplace!")
            .navigationBarTitle(wishlist.name)
            .navigationBarItems(trailing:
                HStack {
                    Button(action: {
                        showingSheet = true
                    }) {
                        Image(systemName: "ellipsis.circle.fill")
                    }
                    .actionSheet(isPresented: $showingSheet) {
                        ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
                                        .default(Text("Delete Wishlist"), action: {
                                            destroyWishlist()
                                        }),
                            .default(Text("Green")),
                            .default(Text("Blue")),
                            .cancel()
                        ])
                    }
                    .foregroundColor(Color("Rye"))
                }
            )
            .listStyle(InsetGroupedListStyle())
    }
    
    func destroyWishlist() {
        AF.request("http://localhost:3000/wishlist/\(wishlist.id)", method: .delete).responseJSON { response in
        switch response.result {
            case .success:
                print("successful")
        case let .failure(_):
                print("error")
            }
        }
    }
}
  • Are you looking for `dismiss()` on `\.presentationMode`? https://stackoverflow.com/questions/56513568/ios-swiftui-pop-or-dismiss-view-programmatically – jnpdx Apr 05 '21 at 04:06

1 Answers1

1

Do you want to go to the previous navigation view? If so, try the following:

1. First, add an environment variable.

@Environment(\.presentationMode) var presentationMode

2. Put the following code where you want the dismiss.

self.presentationMode.wrappedValue.dismiss()

example:

struct WishlistView: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var showingSheet = false
    @State private var show_modal: Bool = false
    let wishlist: Wishlist

    var body: some View {
        Text("Hello, Marketplace!")
            .navigationBarTitle(wishlist.name)
            .navigationBarItems(trailing:
                HStack {
                    Button(action: {
                        showingSheet = true
                    }) {
                        Image(systemName: "ellipsis.circle.fill")
                    }
                    .actionSheet(isPresented: $showingSheet) {
                        ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
                                        .default(Text("Delete Wishlist"), action: {
                                            destroyWishlist()
                                        }),
                            .default(Text("Green")),
                            .default(Text("Blue")),
                            .cancel()
                        ])
                    }
                    .foregroundColor(Color("Rye"))
                }
            )
            .listStyle(InsetGroupedListStyle())
    }
    
    func destroyWishlist() {
        AF.request("http://localhost:3000/wishlist/\(wishlist.id)", method: .delete).responseJSON { response in
        switch response.result {
            case .success:
                print("successful")
                self.presentationMode.wrappedValue.dismiss()
        case let .failure(_):
                print("error")
            }
        }
    }
}
Youngho Joo
  • 159
  • 3