50

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!

John Topley
  • 113,588
  • 46
  • 195
  • 237
Simon Barkhuizen
  • 846
  • 1
  • 9
  • 18

4 Answers4

77

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks! That actually explained alot! :) – Simon Barkhuizen Feb 07 '12 at 13:15
  • I did (Myclass *)nextViewController = segue.destinationViewController; nextView.textfield1.text = @"Test"; but xcode has a problem with (Myclass *)nextViewController – Simon Barkhuizen Feb 07 '12 at 13:22
  • MyClass has to be a subclass of UIViewController. Segues are for new view controllers, not views. – jrturton Feb 07 '12 at 13:25
  • Thanks for your help, I ment UIViewController, not views, sorry for confusion. – Simon Barkhuizen Feb 07 '12 at 13:28
  • I get "Use of undeclared Identifier "nextViewController" so I gave the ViewController in the IB that identifier but it still says that :( – Simon Barkhuizen Feb 07 '12 at 13:30
  • Sorry had the code in my answer wrong, typing on the phone! Please see updated code. – jrturton Feb 07 '12 at 13:33
  • Thanks!! And no worries :-) Enjoy your iPhone! Don't have money to buy one... yet. – Simon Barkhuizen Feb 07 '12 at 13:38
  • @jrturton , what's the difference between link this to buttons or selection of table rows OR call it in code using performSegueWithIdentifier:sender:? Thanks – Jake Lin Aug 20 '13 at 01:54
  • @jrturton I have a question on SO http://stackoverflow.com/questions/18306589/nsobjectnsobject-doesnotrecognizeselector-crash-when-call-viewcontroller. It looks like I want to call performSegueWithIdentifier:sender: programmatically, but prepareForSegue:sender: method still got called by some where else. Would you please advice some hint? Thanks. – Jake Lin Aug 20 '13 at 02:00
  • If anybody was wondering how to create a segue in the storyboard check http://stackoverflow.com/a/17012857/423646 – ThE uSeFuL Oct 13 '14 at 06:50
9

Most segues are initiated automatically as the result of some user interaction. For instance, if you have a segue that is wired up from a button to a scene in a storyboard, when the button is tapped the segue will automatically initiate.

Occasionally, it makes sense to trigger a segue programmatically - e.g. you have a High Scores scene that is displayed when the user wins a round of a game. There's no way to express the concept of winning in the storyboard itself, so you can instead create a segue, assign an identifier to it, and invoke -performSegueWithIdentifier:sender: at runtime.

The other segue related method on UIViewController, -prepareForSegue:sender:, is the method you should override to perform any customization on the destination view controller.

retainCount
  • 4,478
  • 1
  • 22
  • 14
3

In prepareForSegue:sender: you get a chance to configure the destinationViewController: that's where you'd pass it the data it needs. It's discussed in Cocoa Application Competencies for iOS.

-1

Today I ran into the issue of performSegueWithIdentifier: not executing due to the fact of not having set a delegate queue on my URL session.

So by any chance, check if you are actually setting a delegate queue when creating your URLSession, else URLSession will create it's own.

urlSession = [NSURLSession sessionWithConfiguration:sessionConfigObject
                                           delegate:self
                                      delegateQueue:[NSOperationQueue mainQueue]];

I mention this here because I quite often see URLSession handling ending up calling some sort of UI related activity. And performSegue needs to be executed on main, or else it will do just nothing.

Jeroen Leenarts
  • 502
  • 4
  • 11