Pass data through unwind segue

I have been looking all over for an answer for this…I do not know how to pass data through an unwind segue. I know I am supposed to use prepareForSegue but I am not sure what I am supposed to put in. I am trying to pass a variable (myLabel) from a UIPickerView in one class:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myLabel.text = pickerData[row]
}

to another class. I have the unwind segue code already but do not know where to put the prepareForSegue and what to put in it.

  • Disable swipe back gesture in Swift
  • How to remove a cell from table
  • How to get Core Data object from specific Object ID?
  • Load data from a plist to two TableViews
  • How to assure that operations in an OperationQueue are finished one after another
  • Set Background Gradient on Button in Swift
  • Let me know if my question needs more clarification.

    2 Solutions Collect From Internet About “Pass data through unwind segue”

    If I understand your question correctly, you should do something like this:

    class MyPickerController : UIViewController {
      @IBOutlet weak var myLabel: UILabel!
    
      var pickerData:[String]
    
      func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        myLabel.text = pickerData[row]
        performSegueWithIdentifier( "mySegueName", sender: self )
      }
    
      func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let myController = segue.destinationController as? MyDestinationViewController {
          myController.text = myLabel.text
      }
    }
    

    That will pass the label text to the MyDestinationViewController instance.
    Note that I don’t pass the UILabel object, but the text.
    You can not place that UILabel object in the destination controller interface, but you can use the text an set another label with it, for example.

    prepareForSegue is called in the class of the view controller that you will be unwinding from (the view controller that was previously pushed or presented, and is now being popped or dismissed). The destination view controller of the segue is the view controller that will appear as a result.