Swift:点击按钮后关闭工作表不起作用

问题描述 投票:0回答:1

单击“Hinzufügen”按钮(德语)后,使用“Fertig”按钮关闭“AddView”不再起作用。然而,如果事先没有点击“Hinzufügen”,“Fertig”按钮就会起作用。这是代码:

import SwiftUI
import SwiftData

class DestinationModel: ObservableObject {
    @Published var names: String = ""
    @Published var shops: String = ""
    @Published var cuts: String = ""
    @Published var prices: String = ""
    
    
    @Published var showingAddView: Bool = false
    
    
}




struct ContentView: View {
    
    @StateObject var destinationModel: DestinationModel = DestinationModel()
    @Environment(\.modelContext) var context
    @Query var destinations: [Destination]
    
    
    
    
    
    
    var body: some View {
        NavigationStack {
            ZStack {
                Color(red: 242/255, green: 242/255, blue: 247/255)
                    .ignoresSafeArea(.all)
                
                    VStack {
                        HStack {
                            Text("Produkt")
                            Text("Shop")
                            Text("Sparvorteil")
                            Text("Preis")
                        }
                        
                        List {
                            if destinations.isEmpty {
                                ContentUnavailableView(label: {
                                    Label("Keine Produkte eingetragen", systemImage: "cart.badge.plus")
                                }, description: {
                                    Text("Geplante Einkäufe werden hier angezeigt.")
                                }, actions: {
                                    Button("Hinzufügen") {
                                        destinationModel.showingAddView = true
                                    }
                                    .sheet(isPresented: $destinationModel.showingAddView, content: {
                                        AddView(destinationModel: destinationModel)
                                    })
                                })
                                
                            } else {
                                ForEach(destinations) { destination in
                                    HStack {
                                        Text(destination.name)
                                        Text(destination.shop)
                                        Text(destination.cut)
                                        Text(destination.price)
                                    }
                                    
                                    
                                }
                                .onDelete { IndexSet in
                                    for index in IndexSet {
                                        context.delete(destinations[index])
                                    }
                                }
                        }
                    }
                    .navigationBarTitle("Geplante Einkäufe")
                        
                         
                        
                        
                        
                    }
                }
            
            }
            
        
        }
                
    }
    

struct AddView: View {
    
    
    @ObservedObject var destinationModel: DestinationModel
    @Environment(\.modelContext) var context
    @Environment(\.dismiss) var dismiss
    @Environment(\.presentationMode) var presentationMode
    
    
    var body: some View {
        
        VStack {
            HStack {
                Button("Fertig") {
                    presentationMode.wrappedValue.dismiss()
                }
                .padding(.top, 20)
                .padding(.leading, 20)
                .font(.title3)
                Spacer()
            }
            Spacer()
        }
        
        VStack(alignment: .center) {
            TextField("Name", text: $destinationModel.names)
                .padding(.leading, 20)
                .frame(width: 280, height: 40, alignment: .trailing)
                .background(Color.gray)
                .foregroundStyle(Color.black)
                .font(.title2)
                .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
            TextField("Shop", text: $destinationModel.shops)
                .padding(.leading, 20)
                .frame(width: 280, height: 40, alignment: .trailing)
                .background(Color.gray)
                .foregroundStyle(Color.black)
                .font(.title2)
                .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
            TextField("Preis", text: $destinationModel.prices)
                .padding(.leading, 20)
                .frame(width: 280, height: 40, alignment: .trailing)
                .background(Color.gray)
                .foregroundStyle(Color.black)
                .font(.title2)
                .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
            TextField("Vergünstigung", text: $destinationModel.cuts)
                .padding(.leading, 20)
                .frame(width: 280, height: 40, alignment: .trailing)
                .background(Color.gray)
                .foregroundStyle(Color.black)
                .font(.title2)
                .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
            Spacer()
        }
        
        Button("Hinzufügen") {
            let kauf = Destination(name: destinationModel.names, shop: destinationModel.shops, cut: destinationModel.cuts, price: destinationModel.prices)
            context.insert(kauf)
            
            
            
            destinationModel.cuts = ""
            destinationModel.shops = ""
            destinationModel.prices = ""
            destinationModel.names = ""
            
        }
        
        
    }
}


#Preview {
    ContentView()
}

我用“presentationMode.wrappedValue.dismiss()”尝试了几件事,但它从未起作用。其他人有解决方案吗?提前谢谢你。:)

swift button swiftui swift-data
1个回答
0
投票

由于您使用的是

NavigationStack
,因此您必须面向 iOS 16 或更高版本。在这种情况下,有更好的方法来消除视图。

自 iOS 15 起,您可以使用环境值

dismiss
来关闭视图 - 文档 甚至包含一个示例来展示如何将其用于工作表。您已将此环境值包含在
AddView
中,但您似乎并未使用它。

所以我建议进行以下更改:

  1. ContentView
    中,将
    .sheet
    修改器移动到父级
    ZStack
    :
NavigationStack {
    ZStack {
        // content as before, except for the .sheet modifier on the Button
    }
    .sheet(isPresented: $destinationModel.showingAddView) { // <- now here instead
        AddView(destinationModel: destinationModel)
    }
}
  1. AddView
    中,使用
    dismiss()
    作为按钮操作:
@Environment(\.dismiss) private var dismiss
// @Environment(\.presentationMode) var presentationMode // not needed
Button("Fertig") {
    // presentationMode.wrappedValue.dismiss()
    dismiss()
}

或者,您可以重置控制工作表可见性的布尔绑定:

Button("Fertig") {
    // presentationMode.wrappedValue.dismiss()
    // dismiss()
    destinationModel.showingAddView = false // works too
}
  1. 添加父容器到
    AddView

body
AddView
包含 2 个
VStack
和顶层的
Button
。您可能需要用
VStack
包围它们,以定义视图的布局:

// AddView

var body: some View {
    VStack { // <- ADDED

        VStack {
            // content as before
        }

        VStack(alignment: .center) {
            // content as before
        }

        Button("Hinzufügen") {
            // content as before
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.