Brian Love
Angular + TypeScript Developer in Denver, CO

Segue Identifiers

Reading time ~2 minutes

How many times do you declare the same string for a segue identifier in your view controller’s code? Perhaps just one, or twice, or more? I often use the same identifier string multiple times in my code.

While this might be OK when you are just declaring it once, what about two or more times? Let me explain how I use a simple static string for storing my segue identifiers.

What am I talking about?

I am referring to using a segue string when manually performing a segue, such as:

[self performSegueWithIdentifier:@"ToExplanation" sender:self];

Here we have defined a segue in our storyboard, and we gave it an identifier string of ToExplanation. We can then manually trigger that segue to start from within our view controller code by calling the performSegueWithIdentifier:sender: method. The thing to keep in mind here is that when we using the @ToExplanation shortcut for creating a new NSString object, we are still creating a new object instance. This isn’t so bad if we are just doing it once, but what about doing it over and over?

For example, we may then use the same segue identifier again in our prepareForSegue:sender: method, such as:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"ToExplanation"]) {
    ExplanationViewController *vc = segue.destinationViewController;
    vc.someProperty = kSomeValue;
  }
}

In this instance we are again declaring a new NSString object, initialized with the value of “ToExplanation”. This is the second instance of the same string. hat if we use this segue identifier string again somewhere else in our view controller? How many times should we be creating this same string.

Further, what if we decide to later rename the segue identifier? We now have to do a find-and-replace to ensure that we update all instances of this string in our code. That doesn’t seem like a good idea to me.

The solution

The solution is simple. Just define an instance variable at the top of your view controller. You would define this after the @implementation keyword. You might put this right after your @synthesize statements, if you are still using them (remember, iOS 6 introduced default synthesis).

In my example, I define a static NSString instance to store the value of the segue identifier:

NSString *const segueToExplanationView = @"ToExplanation";

I can then use this string for comparison when performing a segue:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:segueToExplanationView]) {
    ExplanationViewController *vc = segue.destinationViewController;
    vc.someProperty = kSomeValue;
  }
}

EDIT: I updated the post to use a const variable rather than a static-scoped variable. Thanks Joony!

Brian Love

Hi, I'm Brian. I am interested in TypeScript, Angular and Node.js. I'm married to my best friend Bonnie, I live in Denver and I ski (a lot).