0

So I’m working on this shopping list and basically I have a list of tile views that have swipe actions.

The bought and delete actions work perfectly, but I am unable to get the edit button to work. The edit button pulls up a form already filled with the item’s information so you can edit it.

The code for the button works, when I make the tile itself a button and tap the tile the edit form comes up.

Is there a limit to swipe actions that limit you pulling up a form in a swipe action? Here is the code for the list. Thanks! (Program is coded using swift ui and swift)

ForEach(Item) { madeItem in
    FoodListTileView(foodItem: madeItem)
        .swipeActions(edge: .leading) {
            Button {
                madeItem.justBought()
                FoodDataController.shared.saveItem(madeItem: madeItem)
            } label: {
                Label("Just Bought", systemImage: "checkmark.square")
            }
            .tint(.green)
        }
        .swipeActions(edge: .trailing) {
            Button { FoodDataController.shared.deleteItem(madeItem: madeItem)} label : {
                Label("Delete", systemImage:"trash")
            }
            .tint(.red)

            Button(action: {showingCreateView = true})  {

                Label("Edit", systemImage: "pencil.circle")

            }
            .sheet(isPresented: $showingCreateView) {
                AddItemView(Item: madeItem)
            }
            .tint(.orange)
        }
    }
}
.listStyle(.plain)
2
  • 1
    Move your sheet from Button to the end of ForEach and use sheet(item to pass data. Commented Jul 2, 2022 at 14:00
  • I'm not sure if this helps...I had a simultaneous drag gesture, when i removed it everything worked fine. Commented Dec 29, 2022 at 10:43

1 Answer 1

0

@Raja is absolutely right. The .sheet inside swipeAction doesn't work, it has to be outside. Better even outside of the ForEach, as otherwise it will always only show the last value of the forEach.

If it's outside the ForEach, you have to use a different way to let sheet know which data to display: .sheet(item:) which is the better alternative in most cases anyway. For that go from @State var showingCreateView: Bool to something like @State var editingItem: Item? where Item should be your items Class. And in the button action, give it the current item value.

Here is the amended code:

// test item struct
struct Item: Identifiable {
    let id = UUID()
    var name: String
}


struct ContentView: View {
    
    // test data
    @State private var items = [
        Item(name: "Butter"),
        Item(name: "Honey"),
        Item(name: "Bread"),
        Item(name: "Milk"),
        Item(name: "Ham")
    ]
    
    @State private var editingItem: Item? // replace Item with your item type
    
    var body: some View {
        List {
            ForEach(items) { madeItem in
                Text(madeItem.name)
                
                    .swipeActions(edge: .leading){
                        Button {
                        } label: {
                            Label("Just Bought", systemImage: "checkmark.square")
                        }
                        .tint(.green)
                    }
                
                    .swipeActions(edge: .trailing){
                        Button {
                        } label : {
                            Label("Delete", systemImage:"trash")
                        }
                        .tint(.red)
                        
                        Button {
                            editingItem = madeItem // setting editingItem (anything else but nil) brings up the sheet 
                        } label: {
                            Label("Edit", systemImage: "pencil.circle")
                        }
                        .tint(.orange)
                    }
            }
        }
        .sheet(item: $editingItem) { item in  // change to item: init here
            Text("Edit: \(item.name)")
        }
        .listStyle(.plain)
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.