Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding performSegueWithIdentifier

Can someone more knowledgeable than I explain performSegueWithIdentifier:sender: for me? I need to switch views (and classes) and also carry a few NSStrings and IDs over to that view's class. I was wondering if this is possible with performSegueWithIdentifier:sender:

Thanks!

like image 603
Simon Barkhuizen Avatar asked Feb 07 '12 12:02

Simon Barkhuizen


People also ask

How do you use segue?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How do I segue between view controllers?

The segue needs to connect from the view controller itself so nothing else triggers it. To create a segue from the controller Control-drag from the View Controller icon to the Exit icon. Give this new segue the identifier unwind to reference it from the code.

What is the significance of the method prepare for sender :)?

prepare(for:sender:)Notifies the view controller that a segue is about to be performed.


1 Answers

First, you have to have set up the segue in your storyboard and give it the appropriate identifier. (Click on the segue (left panel) and then click on Attributes (right panel).

You can then link this to buttons or selection of table rows from your storyboard, or you can call it in code using performSegueWithIdentifier:sender:.

After this, your view controller will be sent the prepareForSegue:sender: message. You override this method in your view controller subclass, and can configure the target view controller as follows:

TargetViewController *targetVC = (TargetViewController*)segue.destinationViewController; targetVC.string1 = string1; 

And so forth. The sender in this method will be the object that you use as the sender in the original method call.

like image 110
jrturton Avatar answered Sep 24 '22 17:09

jrturton