简体   繁体   中英

How can I manually turn off edit mode when the last item in a List gets deleted?

I have a simple view in my app that displays a list of checklists. When there are no checklists I display a placeholder view.

The issue I'm having is that when I delete the last item in the list, the EditButton() doesn't switch back to "Edit"; it still says "Done," even thought the list is empty If I then add a new item, the list appears again but in edit mode (a bad UX).

Is there a way to manually "turn off" edit mode after the last item in the list gets deleted?

Image: https://i.stack.imgur.com/lE1yu.png

import SwiftUI

struct HomeView: View {
    // MARK: - PROPERTIES
    @EnvironmentObject var checklistsVM: Store
    @StateObject var homeVM: HomeViewModel = HomeViewModel()
    
    
    // MARK: - BODY
    var body: some View {
        NavigationView {
            Group {
                if(checklistsVM.checklists.isEmpty) {
                    EmptyList()
                        .environmentObject(homeVM)                    
                } else {
                    HomeListResults()
                        .environmentObject(homeVM)
                }
            }
            .navigationBarTitle("Checklists", displayMode: .large)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

You can turn off the edit mode using the Environment value:

    @Environment(\.editMode) private var editMode

    var body: some View {
        Button("Turn off edit mode") {
            editMode?.wrappedValue = .inactive
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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