Calling performSeguewithIdentifier doesn't call shouldperformseguewithIdentifier
Solution 1:
This behaviour is perfectly natural, for the following reasons:
1) shouldPerformSegueWithIdentifier
is used to make sure that a segue that has been set up in Storyboards should be triggered, so it only gets called in the case of Storyboard Segues and gives you the chance to not actually perform the segue.
2) When you call performSegueWithIdentifier
yourself, shouldPerformSegueWithIdentifier
is not called because it can be assumed that you know what you are doing. There would be no point in calling performSegueWithIdentifier
but then return a NO
from shouldPerformSegueWithIdentifier
.
Solution 2:
@nburk answer is absolutely correct.
However I understand that in some situations it could be useful if shouldPerformSegueWithIdentifier:sender:
would be called anyway, also when a call to performSegueWithIdentifier:sender:
is made in code.
For instance we want to make some validations to decide whether performing a segue or not and we want to keep this logic in a single place and not duplicating all over the place conditions like the following:
if (self.shouldPerformSegue) {
[self performSegueWithIdentifier:identifier sender:sender];
}
This can be easily achieved overriding performSegueWithIdentifier:sender:
as follows:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([self shouldPerformSegueWithIdentifier:identifier sender:sender]) {
[super performSegueWithIdentifier:identifier sender:sender];
}
// otherwise do nothing
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
return self.shouldPerformSegue;
}
This way you can use shouldPerformSegueWithIdentifier:sender:
to define your logic to allow/deny both IB and code triggered segues.
Solution 3:
As the answer above. If you call performSegueWithIdentifier
then shouldPerformSegueWithIdentifier
is not called.
As an example:
Lets say you have an embedded segue inside a container view in order to show some images that you can swipe through. And embedded segues gets fired right away when you VC has loaded. But if you would have to download the images from an remote API your app would crash since there wouldnt be any images to display in the embedded segue/container view.
In this case shouldPerformSegueWithIdentifier
would be needed.
You could setup a boolean value that you check in shouldPerformSegueWithIdentifier
if its false return false and your segue wont be fired. And once your app has downloaded the images you could call performSegueWithIdentifier