为什么我的导航视图/导航堆栈上有这种奇怪的行为

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

我尝试简化代码,在我的代码中,我有一个NavigationStack,如下所示:

查看礼物:

struct MainView: View {

var body: some View {

NavigationStack{
            ZStack{

                // change view base on index
                switch selectedIndex { 
                case 0 : Text("HOME").onAppear {hideBar = false}
                case 1 : Logbook(dm: dm).onAppear {hideBar = false}
                case 2 : AddFlightView(dm: dm, selectIndex: $selectedIndex, hideBar: $hideBar).onAppear {hideBar = true} // <——————————
                case 3 : Text("Flights").onAppear {hideBar = false}
                case 4 : More(dm: dm).onAppear {hideBar = false}
                default: Text("Default").onAppear {hideBar = false}
                }

}
}
}

在 AddFlightView 中有一个列表,用户可以在其中选择某些输入的类型: 有一个导航链接可以从飞机列表中进行选择。

//simplify code
struct AddFlightView: View {
var body: some View {
List{
 Section("Airplane"){
                
                NavigationLink {
                    AirplanePick(planePick: $airplanePick, dm: dm)
                } label: {
                    HStack{
                        Text("Airplane:")
                        Spacer()
                        Text("\(airplanePick?.registration ?? "")")
                    }
                }
            }
}
}
}

飞机选择视图如下:

struct AirplanePick: View {
    @State var addNewPlane = false
    @Environment (\.modelContext) var mc
    @Query(sort:\.registration, order: .forward) var planes: [Aircraft]
    @Binding var planePick: Aircraft?
    let dm: DataManager
    @Environment(\.presentationMode) var presentationMode
    @State var searchQuery: String = ""
    @State var opendetailsSheet = false
    
    var filterPlane : [Aircraft] {
        if searchQuery.isEmpty {
            return planes
        }
        
        let filterPlanes = planes.compactMap { plane in
            let containQuery = plane.registration.range(of: searchQuery,options: .caseInsensitive) != nil
            
            
            let typeQuery = plane.type?.range(of: searchQuery, options: .caseInsensitive) != nil
            
            return (containQuery || typeQuery)  ? plane : nil
        }
        
       
        
        return filterPlanes
        
    }
    var body: some View {
        
        List {
            Text("planes are \(planes.count)")
            
            ForEach(filterPlane, id: \.self){ plane in
                             
                
                Button {
                    planePick = plane
                 
                    self.presentationMode.wrappedValue.dismiss()
                } label: {
                    HStack{
                        VStack(alignment: .leading, content: {
                            Text("Registration").foregroundStyle(.secondary).font(.subheadline)
                            Text(plane.registration).bold()
                        })
                        Spacer()
                        VStack(alignment: .trailing, content: {
                            Text("Type").foregroundStyle(.secondary).font(.subheadline)
                            Text(plane.type ?? "")
                                
                        })

                        
                        ///<———————— issue here when return back from this navigation view
                        Button(action: {
                            opendetailsSheet.toggle()
                        }) {
                            Image(systemName: "info.square.fill")
                                .font(.title)
                                .foregroundColor(.blue)
                        }
                        .padding(.trailing, 10)
                        .navigationDestination(isPresented: $opendetailsSheet) {
                            DetailsPlaneView(plane:plane)
                        }
                        
                    }
                }

            }
            .onDelete { IndexSet in
                
                IndexSet.forEach { index in
                    let airplane = planes[index]
                    mc.delete(airplane)
                }
                
            }
           
           
        }.toolbar {
            NavigationLink("Add New") {
                AddNewAirplane()
            }
            
        }
        .searchable(text: $searchQuery, prompt: "Search Airplane")
        
        .overlay {
            if filterPlane.isEmpty {
                ContentUnavailableView.search
            }
        }
        
        
    }
    

}


当我从“详细信息飞机”视图返回时,AirplanePick 中的列表完全是空的,请参阅我的礼物,为什么?怎么了? 是什么导致我的导航链接出现这种奇怪的行为。

谢谢

swift swiftui navigationview swiftui-navigationstack
© www.soinside.com 2019 - 2024. All rights reserved.