SwiftUI:EditButton 在滑动删除操作后显示 "Done"

SwiftUI: EditButton says "Done" after swipe to delete action

为什么在我使用“滑动删除”删除一个 scrum 后,EditButton 显示“完成”(而不是“编辑”)?

我尝试在 onDelete 中将 editMode 更改为 .inactive,但即使这样也不起作用。

为什么这不起作用,我能以某种方式修复它吗? 是新的错误吗?

struct ScrumsView: View {
    @Binding var scrums: [DailyScrum]
    @Environment(\.scenePhase) private var scenePhase
    @State private var isPresentingNewScrumView = false
    @State private var newScrumData = DailyScrum.Data()
    let saveAction: ()->Void
    
    var body: some View {
        List {
            ForEach($scrums) { $scrum in
                NavigationLink(destination: DetailView(scrum: $scrum)) {
                    CardView(scrum: scrum)
                }
                .listRowBackground(scrum.theme.mainColor)
            }
            .onDelete { indexSet in
                scrums.remove(atOffsets: indexSet)
            }
        }
        .navigationTitle("Daily Scrums")
        .toolbar {
            ToolbarItemGroup(placement: .navigationBarLeading) {
                EditButton()
            }
            ToolbarItemGroup(placement: .navigationBarTrailing) {
                Button {
                    isPresentingNewScrumView = true
                } label: {
                    Image(systemName: "plus")
                }
                .accessibilityLabel("New scrum")
            }
        }
        .sheet(isPresented: $isPresentingNewScrumView) {
            NavigationView {
                DetailEditView(data: $newScrumData)
                    //.navigationTitle(Text("New Scrum"))
                    .toolbar {
                        ToolbarItemGroup(placement: .cancellationAction) {
                            Button("Dismiss") {
                                isPresentingNewScrumView = false
                                newScrumData = DailyScrum.Data()
                            }
                        }
                        ToolbarItemGroup(placement: .confirmationAction) {
                            Button("Add") {
                                let newScrum = DailyScrum(data: newScrumData)
                                scrums.append(newScrum)
                                isPresentingNewScrumView = false
                                newScrumData = DailyScrum.Data()
                            }
                        }
                    }
            }
        }
        .onChange(of: scenePhase) { phase in
            if phase == .inactive { saveAction() }
        }
    }
}

似乎 EditButton 与这种使用 @StateObject 声明存储对象的新方法不兼容。我有想法尝试恢复到创建商店对象的旧方法,令人惊讶的是它解决了这个问题。将 ScrumStore 更改为

class ScrumStore: ObservableObject {
    @Published var scrums: [DailyScrum] = []
    
    static var shared = ScrumStore()

并将 ScrumdingerApp 更改为:

struct ScrumdingerApp: App {
    private var store = ScrumStore.shared
    
    var body: some Scene {
        WindowGroup {
            NavigationView {
                ScrumsView(store: store) {

ScrumsView到这个:

struct ScrumsView: View {
    //@Binding var scrums: [DailyScrum]
    @ObservedObject var store: ScrumStore

...

var body: some View {
    List {
        ForEach($store.scrums) { $scrum in

并将另一个 scrums 也更改为 store.scrums

这使得编辑按钮可以正常工作以滑动删除

我还会通过将商店对象设为环境对象来改进代码,例如

struct ScrumdingerApp: App {
    private var store = ScrumStore.shared
    
    var body: some Scene {
        WindowGroup {
            ContentView()
            .environmentObject(store)
        }
}

struct ContentView: View {
    @EnvironmentObject var store: ScrumStore
   
...

我还会改进模型结构的加载和保存方式,例如我会把它加载到对象的初始化中,并有一个函数来保存它。该函数可以在场景阶段或模型发生变化后调用。在进行过多的加载和保存之前,您可能想尝试 FileDocument,它会为您完成所有这些工作,并免费为您提供 iCloud 驱动器支持。推荐文档app WWDC视频学习,每年都有新的