WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: EditButton() is broken after presentation and dismissal of sheet

Forums > 100 Days of SwiftUI

Description of the problem: Edit-Mode is working at first. But: After the sheet was presented once, it's not working anymore. It looks like this:

Edit Mode Problem

This is the code of ContentView:

NavigationView {
            List {
                ForEach(expenses.items) { item in
                    HStack {
                            VStack(alignment: .leading) {
                                Text(item.name).font(.headline)
                                Text(item.type)
                            }

                            Spacer()
                            Text("$\(item.amount)")
                                .foregroundColor(item.amount <= 10 ? .green : item.amount <= 100 && item.amount > 10 ? .yellow : .red)
                        }
                } .onDelete(perform: removeItems)
            } .sheet(isPresented: $showingAddExpense) {
                AddView(expenses: self.expenses)
            } .navigationBarTitle(Text("iExpense"))
            .navigationBarItems(leading: EditButton(),
                                trailing:
                                    Button(action: {
                                        self.showingAddExpense = true
                                    }) {
                                        Image(systemName: "plus")
                                            .font(.title)
                                    })
        }

1      

I think it to do with struct ExpenseItem that you are not showning and to do with id (if you following the tutorial) are you getting a warning "Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten"

If so change the struct to this

struct ExpenseItem: Identifiable, Codable {
    let id: UUID
    let name: String
    let type: String
    let amount: Double

    init(id: UUID = UUID(), name: String, type: String, amount: Double) {
        self.id = id
        self.name = name
        self.type = type
        self.amount = amount
    }
}

1      

Thank you for your answer! Unfortunatelly this was not the solution. I tried your version of the ExpenseItem-struct. The problem is still there.

It's interesting, because if you start the app, it works. If you open the AddItem-Sheet, even if you just dismiss it with dragging down, the edit-button is not working anymore.

1      

Hi @crabucate

Have you deleted the app from simulator and then made the changes as just making the changes will not do it as the app will still have the UserDefaults.

I think it to do with the id being the same on all the created items, therefore do not know which to delete.

You code look same as mine and work correctly now.

PS I would do the foregroundColor as this. Add this after the var body

func color(for amount: Double) -> Color {
    switch amount {
    case 0..<11:
        return Color.green
    case 10..<101:
        return Color.yellow
    default:
        return Color.red
    }
}

then you can change

Text("£\(item.amount, specifier: "%.2f")")
        .foregroundColor(color(for: item.amount))

1      

Hi, thanks again for the color-tipp. I don't think there is a problem with the id-s. I added two items and printed them in the console:

[iExpenses.ExpenseItem(id: 1EDDC857-A1F4-4F07-9E11-6E4B951EC85B, name: "Number 1", type: "Personal", amount: 1), iExpenses.ExpenseItem(id: 242322AE-A83A-4D79-888C-33BE657CF796, name: "Number 2", type: "Personal", amount: 2)]

Ids are different. Problem still exists. It is not working after the sheet was opened once. I think it is a SwiftUI-bug.

1      

Hi crabucate,

I see your .sheet(isPresented: ) is attached to the list - which happens to be false here because it's initialized to false.

Since you want your sheet for AddView to be presented when the button is pressed, I think it belongs in the action of the button.

1      

That works! Thank you! Does anyone else have this bug?

1      

Unfortunately yes :(

1      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.