How to check if a view controller can perform a segue

Rog picture Rog · Mar 9, 2012 · Viewed 16.8k times · Source

This might be a very simple question but didn't yield any results when searching for it so here it is...

I am trying to work out a way to check if a certain view controller can perform a segue with identifier XYZ before calling the performSegueWithIdentifier: method.

Something along the lines of:

if ([self canPerformSegueWithIdentifier:@"SegueID"])
    [self performSegueWithIdentifier:@"SegueID"];

Possible?

Answer

Michael Miscampbell picture Michael Miscampbell · Feb 15, 2013

To check whether the segue existed or not, I simply surrounded the call with a try-and-catch block. Please see the code example below:

@try {
    [self performSegueWithIdentifier:[dictionary valueForKey:@"segue"] sender:self];
}
@catch (NSException *exception) {
    NSLog(@"Segue not found: %@", exception);
}

Hope this helps.