Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a view controller can perform a segue

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?

like image 669
Rog Avatar asked Mar 08 '12 23:03

Rog


People also ask

How do I segue between view controllers?

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.

How do you trigger a segue?

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.

Should you perform segue?

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.


2 Answers

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.

like image 136
Michael Miscampbell Avatar answered Sep 22 '22 10:09

Michael Miscampbell


- (BOOL)canPerformSegueWithIdentifier:(NSString *)identifier {     NSArray *segueTemplates = [self valueForKey:@"storyboardSegueTemplates"];     NSArray *filteredArray = [segueTemplates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"identifier = %@", identifier]];     return filteredArray.count>0; } 
like image 40
Evgeny Mikhaylov Avatar answered Sep 23 '22 10:09

Evgeny Mikhaylov