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?
As stated in the documentation:
That being said, when you trigger the
segue, normally it’s because it’s assumed that theUIViewControllerwill be able to respond to it with a specificsegue'sidentifier. I also agree with Dan F, you should try to avoid situations where an exception could be thrown. As the reason for you not to be able to do something like this:I am guessing that:
respondsToSelector:only checks if you are able to handle that message in runtime. In this case you can, because the classUIViewControlleris able to respond toperformSegueWithIdentifier:sender:. To actually check if a method is able to handle a message with certain parameters, I guess it would be impossible, because in order to determine if it’s possible it has to actually run it and when doing that theNSInvalidArgumentExceptionwill rise.UIViewControlleris associated with. From theUIViewControllerdocumentation, I wasn’t able to find anything that looks like thatAs for now, I am guessing your best bet it’s to keep going with the
@try@catch@finally.