Fogmeister
Fogmeister

Reputation: 77641

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?

Upvotes: 5

Views: 2838

Answers (3)

RyanTCB
RyanTCB

Reputation: 8224

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#>?

Upvotes: 2

Paul
Paul

Reputation: 1019

If you are trying to pass an object from one view controller to the next you may want to do something like this:

In your parent view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Only if you have multiple segues.
    if ([segue.identifier isEqualToString:@"NameOfYourSegue"]) {
        // Cast necessary to access properties of the destination view controller.
        [(YourChildViewController *)segue.destinationViewController setObject:theObjectToPass];
    }
}

You need to give your segue a name in the storyboard and declare a property for the object to pass in the destination view controller.

Upvotes: 0

Esso
Esso

Reputation: 764

You can always get a segue from the storbyoard, and get the sourceViewController. Something like this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStorboard" bundle:nil];
UIStoryboardSegue *mySegue = [storyboard instantiateViewControllerWithIdentifier:@"mySegue"];
[mySegue sourceViewController]; 

EDIT: Comments beat me to it. ;)

Upvotes: 0

Related Questions