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

Posted

技术标签:

【中文标题】SwiftUI - 视图之间的非自愿导航【英文标题】:SwiftUI - Involuntary navigation between views 【发布时间】:2021-05-19 06:00:51 【问题描述】:

我有两种看法:

父视图首先解码一个捆绑的 JSON 文件,然后将一个对象传递给子视图。然后子视图将对象(以及几个附加对象)保存到 Core Data。

那部分工作正常。问题是保存到核心数据后,子视图然后导航回父视图,我不知道为什么。预期行为是导航回根视图。

父视图:

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.filterself.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)
    

子视图:

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

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

要返回根视图,您可以尝试将“.isDetailLink(false)”添加到 NavigationLink。

SwiftUI: How to pop to Root view

【讨论】:

以上是关于SwiftUI - 视图之间的非自愿导航的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图

带有嵌套导航视图的 Swift UI

SWIFTUI:在视图中导航并返回

如何在 swiftui 中的按钮操作上在视图控制器之间导航?

如何在 SwiftUI 中创建共享导航栏以在多个视图之间进行交互? [关闭]

无法在 swift ui 中单击分段选择器