【问题标题】:Navigate to previous controller SwiftUI导航到上一个控制器 SwiftUI
【发布时间】:2021-07-01 00:06:24
【问题描述】:

我目前处于特定瓶子详细视图的视图控制器中,并为用户提供了一个操作表,使他们能够删除产品。由于他们可以删除此产品,因此一旦成功删除,我需要导航到以前的控制器。我怎样才能在 Swift 中做到这一点?这将在 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")
            }
        }
    }
}

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

您要转到上一个导航视图吗? 如果是这样,请尝试以下操作:

1.首先,添加一个环境变量。

@Environment(\.presentationMode) var presentationMode

2。将以下代码放在您想要关闭的位置。

self.presentationMode.wrappedValue.dismiss()

示例:

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

【讨论】:

    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2013-04-11
    • 2021-03-17
    相关资源
    最近更新 更多