Wednesday, October 28, 2026
HomeiOS Developmentios - My view would not replace after modifying an object of...

ios – My view would not replace after modifying an object of a listing inside an ObservableObject

[ad_1]

I’m new to SwiftUI. Think about I’m constructing an app for a store. I’ve a view that may be a listing of the small print of a product, and two buttons: Pin (add to favorites) and Delete.

My query is in regards to the Pin button (the Delete button works high quality). After I click on on it, the icon is meant to alter to a crammed pin icon as a substitute of the traditional pin icon. And after I return the earlier view, itemizing the 5 newest merchandise and the pinned merchandise (in two totally different sections), this one is meant to replace and add the modified product to the pinned product listing.

However proper now, the pin icon would not change in any respect (however does after I return and are available once more to the product particulars view) and the earlier view would not present the product within the pinned product listing (and I am unable to examine that it seems after I reload the view, as a result of I am unable to return and are available once more to this view as that is the primary view).

I used some tags to point that the view does modify the shop and the merchandise themselves as you’ll be able to see under.

So the view has an attribute for the product itself and one for the shop — the repository that controls the listing of all merchandise:

struct DetailsView: View {
    @StateObject var retailer: Retailer
    @State var product: Product
    
    var physique: some View {
        Record {
            Part {
                VStack(alignment: .main) {
                    Textual content(product.title)
                    
                    Textual content(product.model)
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                }
                .padding(.vertical, 6.0)
            } header: {
                Textual content("Product")
            }
            
            Part {
                Button {
                    product.pinned = !product.pinned
                } label: {
                    HStack {
                        if (product.pinned) {
                            Picture(systemName: "pin.fill")
                            Textual content("Unpin")
                        } else {
                            Picture(systemName: "pin")
                            Textual content("Pin")
                        }
                    }
                    .padding(.vertical, 6.0)
                }
                
                Button {
                    retailer.take away(product.id)
                } label: {
                    HStack {
                        Picture(systemName: "trash")
                        Textual content("Delete")
                    }
                    .foregroundColor(.pink)
                    .padding(.vertical, 6.0)
                }
            }
        }
        .navigationTitle(product.title)
        .navigationBarTitleDisplayMode(.inline)
    }
}

My retailer is simply outlined by one attribute, the listing of merchandise:

class Retailer: ObservableObject {
    @Printed non-public var merchandise: [Product]
    
    …
}

Lastly, a product is outlined by totally different attributes (title, model, pinned):

class Product: Identifiable {
    static non-public var depend: Int = 0
    
    let id: Int
    @Printed var title: String
    @Printed var model: String
    @Printed var pinned: Bool = false
    
    …
}

To recap, my drawback is: my present view is just not up to date (the pin button would not change) and my earlier view is just not up to date after I return to it (the product would not seem within the pinned merchandise listing).

Does anybody have an thought on methods to repair this? Thanks very a lot.

P.S.
My earlier view displaying the pinned gadgets is working high quality besides this. I examined it by initializing my retailer with pinned gadgets they usually seem.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments