Understanding performSegueWithIdentifier

IosObjective CXcodeUikitSegue

Ios Problem Overview


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!

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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.

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimon BarkhuizenView Question on Stackoverflow
Solution 1 - IosjrturtonView Answer on Stackoverflow
Solution 2 - IosretainCountView Answer on Stackoverflow
Solution 3 - Iosuser23743View Answer on Stackoverflow
Solution 4 - IosJeroen LeenartsView Answer on Stackoverflow