1

In a view, there are buttons, radio Buttons and some have same IBAction. I would like to know which one was clicked ? The function resulting to the action has Sender as parameter. Unfortunately, I don't know the name of Sender. In other languages like Delphi, we can know the name of the sender with the following instruction: sender as Button).Name. Is there the same in cocoa swift?

AFK14
  • 48
  • 7
  • No need to use a tag to identify your sender. You can switch the sender. You can see an example here https://stackoverflow.com/a/35691147/2303865 – Leo Dabus Apr 28 '19 at 14:52

1 Answers1

0

You might be looking for the NSView.tag property:

An integer that you can use to identify view objects in your application.

You can give your controls a tag either in code:

yourControl.tag = 10

or in the storyboard:

enter image description here

And then you can check the tag of the sender:

if sender.tag == 10 {
    // that's yourControl!
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • This solve my question. Do the Sender parameter have other properties or methods ? If yes, where can I read them – AFK14 Apr 28 '19 at 08:54
  • @AlfredKrief `sender` is whatever control sent the action. If it is a button, it's a `NSButton`. If it is a text field, it's a `NSTextField`. It will have whatever properties those classes (and their superclasses) define. You can see their documentation just by googling their class names. – Sweeper Apr 28 '19 at 08:59
  • Ok, I understand. I can use the same props. and Methods for sender and the control. – AFK14 Apr 28 '19 at 09:11