user11371080
user11371080

Reputation:

Navigate to previous controller SwiftUI

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")
            }
        }
    }
}

Upvotes: 1

Views: 138

Answers (1)

Youngho Joo
Youngho Joo

Reputation: 179

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")
            }
        }
    }
}

Upvotes: 2

Related Questions