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?
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.
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.
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With