Swift performSegueWithIdentifier sender value

I am trying to understand how the sender value works in segues.

In some places in my code, both work:

performSegueWithIdentifier("mySegue", sender: self) performSegueWithIdentifier("mySegue", sender: sender) 

But what is the difference between having self / sender?

+6
source share
3 answers

As @ iosDev82 reports in its response, the sender is optional, which names the object (if any) that called segue.

If you call segue through code in the view controller, you can pass the view controller (self), or you can pass zero. This is just part of the information that is shared together to prepare ForSegue (again, as stated in iOSDv82).

If you call segue in the IBAction method code, your IBAction may have its own sender parameter (often a button). In this case, you can pass on 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) } 
+12
source

sender is just an argument that is passed along with this function.

This will be obtained later in the prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) function prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) , Where you can get this object and make a decision based on who the sender is.

+2
source

As an example, suppose you want to edit something using IndexPath . You call

performSegue(withIdentifier: "showItem", sender: indexPath)

wherever you want (indexPath is of type IndexPath ).

Then inside prepare(segue:sender:) you will do:

 ... switch segue.identifier { case "showItem"?: // Figure out which row was just tapped if let row = tableView.indexPathForSelectedRow?.row { editRow(segue, row) } else { let selectedIndexPath = sender as! IndexPath editRow(segue, selectedIndexPath.row) } ... 
0
source

All Articles