Search code examples
iosobjective-cuistoryboardsegue

performSegue:withSender: sender object


Just wondering about the sender object.

I know that you can access it in the method prepareForSegue... but is it available at all in the next destinationViewController?

i.e. in the viewDidLoad could I access a segueSender object or something?

I haven't ever seen this in documentation I just thought it might be useful.

EDIT FOR CLARITY

I'm not asking how to perform a segue or find a segue or anything like this.

Say I have two view controllers (VCA and VCB).

There is a segue from VCA to VCB with the identifier "segue".

In VCA I run...

[self performSegueWithIdentifier:@"segue" sender:@"Hello world"];

My question is can I access the @"Hello world" string from VCB or is it only available in the prepareForSegue... method inside VCA?

i.e. can I access the segue sender object from the destination controller?


Solution

  • Yes you can.

    Old question but Ill pop in an answer using swift

    first you call

    performSegueWithIdentifier("<#your segue identifier #>", sender: <#your object you wish to access in next viewController #>)
    

    in prepareForSegue

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            let dest = segue.destinationViewController as! <# your destination vc #>
            dest.<# sentThroughObject #>  = sender
        }
    }
    

    Then in vc you segue to have a var that will accept the passed through object

    class NextViewController: UIViewController {
    
        var <# sentThroughObject #> = AnyObject!()
    
     //cast <# sentThroughObject #> as! <#your object#> to use
    }   
    

    UPDATE: SWIFT 3

    performSegue(withIdentifier: "<#your segue identifier #>", sender: <#Object you wish to send to next VC#>)
    
    
    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
            let destination = segue.destinationController as! <# cast to your destination VC #>
            destination.sentViaSegueObject = sender as? <#your object#> 
        }
    

    In destination VC have a property to accept the sent Object

     var sentViaSegueObject = <#your object#>?