2
                    Button(button1Name, action: messageMe)
                .frame(minWidth:0, maxWidth: 300)
                .padding()
                .foregroundColor(.white)
                .background(Color.gray)
                .cornerRadius(20)
                .tag("button1")
            
             }
         }
    }
}

func messageMe(//get sender) {
    print("Button Tag " //sender.id)
}

How can I get the tag of the button sent, so that I can run an action depending on which button was pressed? I know this is very basic, but cannot find a solution for SwiftUI. I'm back to developing after around 4 years and my beloved Objective-C is no more!

Daniel
  • 31
  • 4
  • passing a paramater to your function (messageMe) can solve this problem – Ouail Bellal Jun 18 '21 at 18:44
  • 1
    Objective-C is still there - you can use it in full range with all iOS frameworks, but if you want to use swift+SwiftUI then you need to learn new concepts and in swift language and in SwiftUI framework. – Asperi Jun 18 '21 at 18:59
  • @Asperi would you recommend I take on Swift+SwiftUI or go back to my (preferred) Objective-C? – Daniel Jun 18 '21 at 21:21

1 Answers1

2

You can do it directly , without any needs of tag like that :

Button("Button title 1") {
  print("Button tapped!")
  messageMe(buttonId :"1")
}
.frame(minWidth:0, maxWidth: 300)
.padding()
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(20)

func messageMe(buttonId : String) {
  // use the buttonId
}
Ouail Bellal
  • 1,554
  • 13
  • 27