Hi all.
In the case of an action created for a button, what's the difference between:
- sender: UIButton and
- sender: Any
Does it make any difference at all? Are there situations where one should be used and not the other?
Thanks!
Hi all.
In the case of an action created for a button, what's the difference between:
Does it make any difference at all? Are there situations where one should be used and not the other?
Thanks!
If you need to know who sent the message inside the IBAction, use sender: UIButton
Otherwise, you can use Any, that will make no difference.
I usually prefer to know who the sender is.
However, if you want to use the IBAction func as a general func,
@IBAction func test(_ sender: Any) {
}then it may be convenient to use any and call the func as
test(self)whatever self is.
In ObjectiveC the class is typically listed as (id). I assume that is what Swift means by "Any". If you know that the action will only be called by a UIButton then you can make that (UIButton *) in ObjectiveC and I assume that is the same as Swift's UIButton. Here's the difference and the reason for adding UIButton (in Objective C and I suspect also in Swift.....
If you want to use a property of the sender that is specific to the class UIButton, like sender.currentTitle, the compiler will object if it does not know that the sender is a UIButton. So you have to 'cast' the sender into UIButton either before the property is referenced or as your question suggests, in the method call itself.
Hi Claude.
If I wanted to have, say, three buttons connected to the same action and have the action call a function, passing it the "sender" as a parameter (so that the function would do different things depending on which button was tapped) , what would be the syntax for the call?
Thanks!
Err... Maybe my question should be "what would be the syntax inside the function to reference the button that was tapped, such as in a switch statement..."?
What I would do would be a bit different, using for instance the tag of the button to differentiale, instead of the button itself.
func doSomethingDependingOnWhoSent(_ senderTag: Int) {
switch senderTag {
case 1 : doForButton1()
case 2 : doForButton2()
case 3 : doForButton3()
default: break
}
}
@IBAction func test(_ sender: UIButton) {
doSomethingDependingOnWhoSent(sender.tag)
}Of course, you could also pass the sender
func doSomethingDependingOnWhoSent(_ sender: UIButton) {
switch sender.tag {
case 1 : doForButton1()
case 2 : doForButton2()
case 3 : doForButton3()
default: break
}
}
@IBAction func test(_ sender: UIButton) {
doSomethingDependingOnWhoSent(sender)
}If you have declared IBOutlets for button1, button2, button3, you could also write:
func doSomethingDependingOnWhoSent(_ sender: UIButton) {
if sender == button1 {
doForButton1()
} else if sender == button2 {
doForButton2()
} else if sender == button3 {
doForButton3()
}
}Does that answer your question ?
Hi Claude.
> Does that answer your question ?
It sure does!!! I'll try out the different approaches. Thank you very much!