Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift performSegueWithIdentifier sender value

Tags:

ios

swift

segue

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?

like image 633
Kiwo Tew Avatar asked Aug 17 '15 19:08

Kiwo Tew


People also ask

What is Sender in Perform segue?

The string that identifies the triggered segue. In Interface Builder, you specify the segue's identifier string in the attributes inspector. This method throws an Exception handling if there is no segue with the specified identifier. sender. The object that you want to use to initiate the segue.

What is performSegue?

The performSegue method calls a segue to be performed from one view to another. Before the segue actually takes place, the prepareForSegue method is called, and if you want to pass data between the views, you'd do it there. The performSegue method doesn't take the parameter you want to send.

What is performSegue in Swift?

How to perform a segue programmatically using performSegue() Swift version: 5.6. Segues are a visual way to connect various components on your storyboard, but sometimes it's important to be able to trigger them programmatically as well as after a user interaction.


1 Answers

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)
}
like image 63
Duncan C Avatar answered Sep 28 '22 05:09

Duncan C