RunSegue: withSender: sender object

It's just interesting to know about the sender object.

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

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

I never saw this in the documentation, I just thought it might be useful.

EDIT FOR CLARITY

I am not asking how to execute segue or find segue or something like this.

Let's say I have two view controllers (VCA and VCB).

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

In VCA, I run ...

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

My question: Can I get access to the string @ "Hello world" from the VCB, or whether it is only available in prepareForSegue... method in VCA?

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

+4
source share
4 answers

So the answer is no.

Thanks for the help.

+1
source

Yes, you can.

Old question, but I don't know how to use 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 go on to have var that will accept the passed 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#> } 

VC destination has property to accept sent object

  var sentViaSegueObject = <#your object#>? 
+1
source

You can always get segue from stock and get sourceViewController . Something like that:

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

EDIT: Comments beat me up;)

0
source

If you are trying to transfer an object from one view controller to the next, you can do something like this:

In the 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 passed in the destination view controller.

0
source

Source: https://habr.com/ru/post/1490663/


All Articles