SwiftUI:当我解锁应用程序时,它会将我带回列表

SwiftUI: When I unlock apps it takes me back to the list

我的申请有问题。当我在内容视图中锁定它然后解锁时,它会将我带回列表。我希望锁定前的视图在解锁后仍然可见。我试图以某种方式获得这种效果但没有成功。请给我一个提示。

更重要的是,如果我点击心形并且该项目被标记为收藏,它也会让我回到列表。在这里我也想在选择心脏后留在视图中。我怎样才能消除它?

这是使用 Apple 示例代码的示例用法,因为您没有提供任何实体信息

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>
    //SceneStorage to preserve the selected item by the user
    @SceneStorage("ContentView.selection") var selection: String?
    var body: some View {
        NavigationView {
            List {
                ForEach(items) { item in
                    //Use the NavigationLink that uses selection
                    NavigationLink(tag: item.objectID.description, selection: $selection,
                                   destination: {
                        //to edit/ observe the item pass it to an @ObservedObject
                        EditItemView2(item: item)
                    }, label: {
                        VStack{
                            Text(item.timestamp!, formatter: itemFormatter)
                        }
                        
                    })
                    Button("delete", action: {
                        withAnimation(.easeOut(duration: 2)){
                            try? viewContext.delete(item)
                        }
                    })
                }.onDelete(perform: deleteItems)
            }
            
        }
        .toolbar {
#if os(iOS)
            ToolbarItem(placement: .navigationBarTrailing) {
                EditButton()
            }
#endif
            ToolbarItem {
                Button(action: addItem) {
                    Label("Add Item", systemImage: "plus")
                }
            }
        }
        Text("Select an item")
        
    }
    
    private func addItem() {
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()
            do {
                try viewContext.save()
            } catch {
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
    
    private func deleteItems(offsets: IndexSet) {
        withAnimation(.easeOut(duration: 2)) {
            offsets.map { items[[=10=]] }.forEach(viewContext.delete)
            
            do {
                try viewContext.save()
            } catch {
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

struct EditItemView2: View{
    //This observes the item and allows changes to the object
    @ObservedObject var item: Item
    var body: some View{
        DatePicker("timestamp", selection: $item.timestamp.bound)
    }
}

这个页面讨论了所有内容

https://developer.apple.com/documentation/uikit/view_controllers/restoring_your_app_s_state_with_swiftui

如果您的视图是第一个创建 UserSettings 对象的视图,我建议您使用 @StateObject 而不是 @ObservedObject——特别是如果该对象是视图并且没有被任何其他视图观察到。