简体   繁体   English

SwiftUI - 视图之间的非自愿导航

[英]SwiftUI - Involuntary navigation between views

I have two views:我有两种看法:

The parent view first decodes a bundled JSON file and then passes an object to the child view.父视图首先解码捆绑的 JSON 文件,然后将 object 传递给子视图。 The child view then saves the object (plus a couple additional objects) to Core Data.然后子视图将 object(加上几个附加对象)保存到 Core Data。

That part works fine.那部分工作正常。 The problem is that after saving to Core Data, the child view then navigates back to the parent view and I'm not sure why.问题是保存到核心数据后,子视图然后导航回父视图,我不知道为什么。 Intended behaviour is to navigate back to the root view.预期行为是导航回根视图。

Parent View:父视图:

struct SelectCityView: View {
    
    @State private var cities = [Destination]()
    @State private var searchText: String = ""
    
    var body: some View {
        
        VStack {
            
            SearchBar(text: $searchText)
            
            List {
                
                // Filter decoded array by search text
                ForEach(cities.filter{self.searchFor($0.city)}.sorted(by: { $0.city < $1.city })) { destination in
                    
                    // Here's where the parent view passes the object to the child view
                    NavigationLink(destination: SelectTravelDatesView(selectedCity: destination)) {
                        Text("\(destination.city), \(destination.country)")
                    }
                }
            }.id(UUID())
            
        }.navigationTitle("Select city")
        .onAppear(perform: {
            
            // Decode JSON data from bundle
            cities = Bundle.main.decode([Destination].self, from: "cities.json")
        })
    }
    
    private func searchFor(_ searchQuery: String) -> Bool {
        return (searchQuery.lowercased(with: .current).hasPrefix(searchText.lowercased(with: .current)) || searchText.isEmpty)
    }
}

Child View:子视图:

struct SelectTravelDatesView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    @State var selectedCity: Destination?
    @State private var startDate = Date()
    @State private var endDate = Date()
    
    var body: some View {
        
        Form {
            Section(header: Text("Trip start date")) {
                
                DatePicker(selection: $startDate, in: Date()..., displayedComponents: .date) {
                    Text("Trip start date")
                }.datePickerStyle(GraphicalDatePickerStyle())
            }

            Section(header: Text("Trip end date")) {
                
                DatePicker(selection: $endDate, in: startDate..., displayedComponents: .date) {
                    Text("Trip start date")
                }.datePickerStyle(GraphicalDatePickerStyle())
            }
        }.navigationTitle("Select dates")
        
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                
                // Save to Core Data
                Button(action: {
                    addItem()
                }, label: {
                    Text("Save")
                })
            }
        }
    }
    
    private func addItem() {
        withAnimation {
            
            let newItem = Trip(context: viewContext)
            
            if let destination = selectedCity {
                
                newItem.timestamp = Date()
                newItem.destination = destination.city
                newItem.startDate = startDate
                newItem.endDate = endDate
                
                do {
                    try viewContext.save()
                } catch {
                    let nsError = error as NSError
                    fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
                }
            }
        }
    }
}

Any help would be greatly appreciated.任何帮助将不胜感激。

to go back to the root view you can try adding ".isDetailLink(false)" to the NavigationLink.到 go 返回根视图,您可以尝试将“.isDetailLink(false)”添加到 NavigationLink。

see

SwiftUI: How to pop to Root view SwiftUI:如何弹出到根视图

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

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