ios – Why the icons cannot change immediately?


These are the three views in my to do list project.
I want to save data, so users’ to do list will still be there after they launch the app.
But whenever I click on the icon “check.circle’ or “circle”, they cannot switch immediately. They will switch after I launch the app again.
I don’t know how to let icon switched immediately.
Thanks in advance!!!

@EnvironmentObject var listViewModel: ListViewModel

@State var newText: String = ""

@State var detents: PresentationDetent = .medium

@State var showSheet: Bool = false

static var item1 = ItemModel(title: "First", isCompleted: false)

var body: some View {    

    NavigationView {
            List {
                ForEach(listViewModel.items) { item in
                    ListRowView(item: item)
                        .onTapGesture {
                            listViewModel.updateItem(item: item). -> *
                        }
                }
                .onDelete(perform: listViewModel.delete)
                .onMove(perform: listViewModel.moveItem)
            }
            .navigationTitle("To Do List")
            .navigationBarItems(
                leading: EditButton(),
                trailing: Button(
                    action: {
                        showSheet.toggle()
                    }, label: {
                    Text("Add")
                })
                .sheet(
                    isPresented: $showSheet,
                    content: {
                        SecondScreen()
                            .presentationDetents([.height(500)])
                }
            ))
            }
    }  



func saveItems() {
   if let encodedData = try? JSONEncoder().encode(items) {
        UserDefaults.standard.set(encodedData, forKey: itemsKey)
    }





let id: String
let title: String
var isCompleted: Bool

init(id: String = UUID().uuidString, title: String, isCompleted: Bool) {
    self.id = id
    self.title = title
    self.isCompleted = isCompleted
}

func updateCompletion() -> ItemModel {.  -> *
    return ItemModel(id: id, title: title, isCompleted: !isCompleted)
}

After I click on the icon, I want to let the icon switch immediately.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img