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?
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.
First, select a segue in your storyboard, then go to the attributes inspector and give it a name such as “showDetail”. Technically the sender parameter is whatever triggered the segue, but you can put whatever you want in there.
Performing seguesNotifies the view controller that a segue is about to be performed. func performSegue(withIdentifier: String, sender: Any?) Initiates the segue with the specified identifier from the current view controller's storyboard file.
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.
- (BOOL)canPerformSegueWithIdentifier:(NSString *)identifier { NSArray *segueTemplates = [self valueForKey:@"storyboardSegueTemplates"]; NSArray *filteredArray = [segueTemplates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"identifier = %@", identifier]]; return filteredArray.count>0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With