简体   繁体   English

导航到上一个 controller SwiftUI

[英]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.我目前正在查看 controller 以获得特定的瓶子详细信息视图,并为用户提供一个操作表,使他们能够删除产品。 Since they can delete this product, I need to navigate to the previous controller once it's successfully deleted.由于他们可以删除此产品,因此一旦成功删除,我需要导航到以前的 controller。 How can I do this in Swift?如何在 Swift 中做到这一点? This will be done in the event of a successful API request.这将在 API 请求成功的情况下完成。

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

Do you want to go to the previous navigation view?是否要将 go 转到上一个导航视图? If so, try the following:如果是这样,请尝试以下操作:

1. First, add an environment variable. 1.首先,添加一个环境变量。

@Environment(\.presentationMode) var presentationMode

2. Put the following code where you want the dismiss. 2. 将以下代码放在您想要关闭的位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM