swift performSegueWithIdentifier sender value

24,768

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.

Share:
24,768
Kiwo Tew
Author by

Kiwo Tew

Updated on July 09, 2022

Comments

  • Kiwo Tew
    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
    Kiwo Tew almost 9 years
    But 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
    Duncan C almost 9 years
    The 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 your prepareForSegue method.
  • nhgrif
    nhgrif almost 9 years
    Furthermore, you could even pass, say, a dictionary with keys and values that you need to set on the destination view controller...
  • Kiwo Tew
    Kiwo Tew almost 9 years
    Thanks, but is it kinda like a replacement for prepareForSegue ? I mean the way I can choose what to pass along
  • Kiwo Tew
    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
    Duncan C almost 9 years
    Argggggh! 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 for prepareForSegue. It's a piece of information that gets passed to prepareForSegue.
  • Kiwo Tew
    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