HomeiOS Developmentios - Methods to outline confirmationDialog with Binding Merchandise as an alternative...

ios – Methods to outline confirmationDialog with Binding Merchandise as an alternative of isPresented one?


That is what I’ve, to current confirmationDialog on faucet:

extension View {
    func confirmDeletion(isPresented: Binding, title: String? = nil, motion: @escaping ()-> Void) -> some View {
        confirmationDialog("", isPresented: isPresented) {
            Button("delete", function: .damaging) { motion() }
        } message: {
            Textual content(title)
        }
    }
}

And it really works completely, however now I must outline and name it base on chosen Merchandise from the listing, not simply by toggle isPresented, so I merely outlined it like this:

extension View {
    // right here is earlier declaration
    func confirmDeletion(merchandise: Binding, title: String? = nil, motion: @escaping (Merchandise)-> Void) -> some View {
        let binding = Binding {
            merchandise.wrappedValue != nil
        } set: { _ in }

        return confirmDeletion(isPresented: binding, title: title) { // name the earlier declaration with overriden motion
            if let merchandise = merchandise.wrappedValue {
                motion(merchandise)
            }
        }
    }
}

However the challenge is that it’s displayed solely as soon as. Once I faucet the identical PlanElementView in a row, it doesnt current dialog second time. Why? Once I faucet completely different PlanElementView it really works.

In code I take advantage of it like this:

@State var selectedPlanForDeletion: Plan?

var physique: some View {
    VStack {
        ForEach(plans) { plan in
            PlanElementView(plan: plan)
                .onTapGesture {
                    selectedPlanForDeletion = plan
                }
        }
    }
    .confirmDeletion(
        merchandise: $selectedPlanForDeletion,
        title: "newTitle"
    ) { plan in
        print(plan)
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments