[ad_1]
In my information object, I’ve a dictionary which specifies what number of buttons I must show on the view. I’m looping over the dictionary to show the buttons with customized model and when a button is clicked, I intend to show a sheet with content material as customized message alert view.
What I’m noticing is when I’ve greater than 1 buttons, sheet opens up with customized message alert view just for first button. For different buttons, I do see sheet opening up, however the code in customized view by no means will get executed.
Code under:
struct CustomActionsView: View {
let dataObject: CustomModel
@State var showingModal: Bool = false
var physique: some View {
VStack {
var actionName = ""
// actions is dictionary the place secret's identify that shall be displayed on the button and worth is identify of the picture I shall be displaying on the button.
ForEach(dataObject.actions.sorted(by: >), id: .key) { key, worth in
let imageName = (key == "Cancel" ? "xmark.circle" : worth)
// Button with customized styling.
CapsuleActionButtonWithImage(actionNameText: key, imageName: imageName, motion: { self.showingModal.toggle(); actionName = key; NSLog("Clicked motion is - (actionName)") })
.sheet (
isPresented: $showingModal,
content material: {
// Solely execute the logic for clicked button.
if key == actionName {
// This perform at all times will get known as and returns appropriate information for the button clicked.
let messageViewDetails = Helper.getMessageDetails(dataObject: dataObject, clickedActionName: actionName)
// This will get known as just for one button.
GeneralAlertMessageView(imageName: messageViewDetails.messageViewimageName, mainMessageText: messageViewDetails.messageText, messageViewButtonStyle: .okButton, showingModal: $showingModal)
}
}
)
Spacer()
}
}
}
}
Code for GeneralAlertMessageView which ought to present up at any time when any buttons are clicked:
struct GeneralAlertMessageView: View {
var imageName: String?
var mainMessageText: String?
var messageViewButtonStyle: MessageViewButtonStyle = .noButton
@Binding var showingModal:Bool
var physique: some View {
VStack {
let _ = NSLog("Inside GeneralAlertMessageView")
// Some UI code
}
}
}
As an example I’ve 3 key-value pairs inside “dataObject.actions” dictionary and therefore I’ll see 3 buttons on the view. Once I click on on first button, actionName will get set to the identify of the button I clicked(inside motion closure for CapsuleActionButtonWithImage) and getMessageDetails will get executed and “Inside GeneralAlertMessageView” will get printed on the console since GeneralAlertMessageView will get displayed on the sheet.
At any time when I click on on 2nd and third buttons, actionName will get set to the identify of the button I clicked and getMessageDetails does get executed for the button I clicked, however I by no means see “Inside GeneralAlertMessageView” getting printed and all I see is clean sheet. Any breakpoints inside GeneralAlertMessageView would not hit both.
Am I doing something flawed? Is there a greater method to open sheet for each button click on inside for loop?
Thanks!
[ad_2]
