@AppStorage 属性 包装器防止关闭视图

@AppStorage property wrapper prevents from dismissing views

我有一个有四 (4) 个视图的应用程序,在第一个视图中,我显示了从 CoreData 中提取的汽车列表,第二个视图在汽车被点击时显示,它显示了每辆汽车的服务.第三个视图在点击服务时出现,它显示了所选服务的详细信息。第四个视图在点击按钮时显示,它显示指定服务的记录。

我遇到的问题是,出于某种原因,如果我在 ServicesView 中使用 @AppStorage 属性 包装器,我无法关闭第四个视图 (RecordsView)。我认为问题不在于 CoreData,但如果您需要查看 Core Data 的代码,请告诉我。

知道为什么在 ServicesView 中添加 @AppStorage 属性 包装器会影响其他视图吗?

汽车视图

    struct CarsView: View {
        @ObservedObject var carViewModel:CarViewModel
        @State private var carInfoIsPresented = false
        
        var body: some View {
            
            NavigationView{
                VStack{
                    List {
                        ForEach(carViewModel.cars) { car in
                            HStack{
                                VStack(alignment:.leading){
                                    Text(car.model ?? "")
                                        .font(.title2)
                                    Text(car.make ?? "")
                                        .foregroundColor(Color(UIColor.systemGray))
                                }
                                NavigationLink(destination: ServicesView(carViewModel: carViewModel, selectedCar: car)){
                                    Spacer()
                                    Text("Services")
                                        .frame(width: 55)
                                        .font(.caption)
                                        .foregroundColor(Color.systemGray)
                                }
                            }
                        }
                    }
                    .listStyle(GroupedListStyle())
                    .navigationBarTitle("Cars")
                    .accentColor(.white)
                    .padding(.top, 20)
                }  
            }
        }
    }

服务视图

    struct ServicesView: View {
        @ObservedObject var carViewModel: CarViewModel
        var selectedCar: Car
        
        // ISSUE: No issues dismissing the RecordsView if I comment this out
        @AppStorage("sortByNameKey") private var sortByName = true

        @State private var selectedService: CarService?

        var body: some View {
            VStack{
                List {
                    ForEach(carViewModel.carServices) { service in
                        HStack{
                            Text(service.name ?? "")
                                .font(.title3)
                            
                            NavigationLink(destination: ServiceInfoView(carViewModel: carViewModel, selectedCar: selectedCar, selectedService: service)){
                                Spacer()
                                Text("Details")
                                    .font(.caption)
                                    .foregroundColor(Color.systemGray)
                            }
                        }
                    }
                }
                .navigationBarTitle(Text("\(selectedCar.model ?? "Services") - Services"))
                .listStyle(GroupedListStyle())
            }
            .onAppear{
                carViewModel.getServices(forCar: selectedCar)
            }
        }
    }

ServiceInfoView

    struct ServiceInfoView: View {
        @ObservedObject var carViewModel: CarViewModel
        @State private var recordsViewIsPresented = false
        @State var selectedCar: Car
        @State var selectedService: CarService
        
        var body: some View {
            VStack{
                Text(selectedService.name ?? "")
                    .font(.largeTitle)
                    .padding(.bottom)
                
                VStack{
                    Button(action: openRecordsView) {
                        Text("Service History")
                    }
                    .padding(10)
                    .background(Color.blue)
                    .foregroundColor(Color.white)
                    .cornerRadius(15)
                }
            }
            .sheet(isPresented: $recordsViewIsPresented){
                RecordsView(carViewModel: carViewModel, selectedService: selectedService)
            }
        }
        
        func openRecordsView(){
            recordsViewIsPresented.toggle()
        }
    }

记录视图

    struct RecordsView: View {
        @ObservedObject var carViewModel: CarViewModel
        @State var selectedService: CarService
        @Environment(\.presentationMode) var presentationMode

        var body: some View {
            NavigationView {
                VStack{
                    List {
                        Section(header: Text("Records")) {
                            ForEach(carViewModel.serviceRecords) { record in
                                HStack{
                                    Text("Service Date:")
                                    Text("\(record.serviceDate ?? Date(), style: .date)")
                                        .foregroundColor(Color(UIColor.systemGray))
                                }
                            }
                        }
                    }
                    .background(Color.purple)
                    .listStyle(GroupedListStyle())
                }
                .navigationBarTitle("Records for \(selectedService.name ?? "")", displayMode: .inline)
                .navigationBarItems(leading: Button("Cancel", action: dismissView))
                .onAppear{
                    carViewModel.getRecords(forService: selectedService)
                }
            }
        }
        
        func dismissView(){
            presentationMode.wrappedValue.dismiss()
        }
    }

NavigationView 只能推送一个详细信息屏幕,除非您在 NavigationLink 上设置 .isDetailLink(false)

仅供参考,我们不在 SwiftUI 中使用视图模型对象,您必须学习正确使用视图结构以及 @State@Binding@FetchRequest 等使安全高效的结构表现得像一个对象。如果您忽略这一点并使用一个对象,您将遇到 Swift 及其值类型旨在防止的错误。有关详细信息,请参阅此答案 MVVM has no place in SwiftUI