swift performSegueWithIdentifier sender value
Solution 1
As @iosDev82 says in his answer, sender is an optional that names the object (if any) that triggered the segue.
If you trigger a segue through code in a view controller, you could pass the view controller (self), or you could pass nil. It's just a piece of information that is passed along to prepareForSegue (again as iOSDv82 says.)
If you trigger a segue in the code of an IBAction method, your IBAction may have it's own sender parameter (frequently a button.) In that case you can pass along the sender parameter to the performSegueWithIdentifier
method.
Example:
@IBAction func buttonAction(sender: UIButton)
{
//In this case the button IBAction takes a pointer to the button as a param.
//Pass it on to the segue in case performWithSegue needs it.
self.performSegueWithIdentifier("someID", sender: sender)
}
Solution 2
sender
is just an argument that gets passed along with this function.
This is received later in the function prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
, where you can get this object and make decision based on who the sender is.
Kiwo Tew
Updated on July 09, 2022Comments
-
Kiwo Tew almost 2 years
I am trying to understand how the sender value works in segues.
In some places in my code both works:
performSegueWithIdentifier("mySegue", sender: self) performSegueWithIdentifier("mySegue", sender: sender)
But what is the difference between having self / sender?
-
Kiwo Tew almost 9 yearsBut this is also a button and it works, what is the difference: @IBAction func Mybutton(sender: AnyObject) { selfperformSegueWithIdentifier("someID", sender: self) } I am trying to look it up in the docs, can't find it : /
-
Duncan C almost 9 yearsThe sender parameter is whatever you want it to be. If you need to know which button the user pressed to trigger the segue, pass the button (sender) along to
performSegueWithIdentifier
. If you need to know the view controller, pass that. If you don't care, pass nil. The info is for you, and will be passed to yourprepareForSegue
method. -
nhgrif almost 9 yearsFurthermore, you could even pass, say, a dictionary with keys and values that you need to set on the destination view controller...
-
Kiwo Tew almost 9 yearsThanks, but is it kinda like a replacement for prepareForSegue ? I mean the way I can choose what to pass along
-
Kiwo Tew almost 9 years@nhgrif Thanks, but is it kinda like a replacement for prepareForSegue ? I mean the way I can choose what to pass along
-
Duncan C almost 9 yearsArgggggh! No, we are talking about the sender parameter, who's purpose is to be passed along to your
prepareForSegue
method. It is in no way a replacement forprepareForSegue
. It's a piece of information that gets passed toprepareForSegue
. -
Kiwo Tew almost 9 years@DuncanC thanks for finally making it clear to me, was an aha moment. Haha sorry! english is not my first languish