1
votes

Question How do I deny a storyboard segue from taking place?

Context: I have a static grouped table view of options on the first view controller that is visible (the main menu if you will).

I purposely show the UI early to give the user a chance to interact and perceive a fast load response. Other operations that might be importing data for example, happen on a background operation queue. Most of my UI functions are in fact, ready to go as soon as you see them.

One menu item (table row) in particular though, isn't ready until some data is imported (I send a notification when all is ready).

While I could remove the storyboard segue for this action, it got me thinking on how I might inhibit a segue from firing because a certain condition wasn't met.

I'll likely allow the segue in practice, and have the destination view controller limit initialization until it receives the notification it is waiting for.

But can one intercept a segue and stop it, programmatically?

3
For future readers without the iOS 5.0 requirement, on iOS 6.0 you can use -[UIViewController shouldPerformSegueWithIdentifier:sender:]Jon Hess
Indeed, a lot of time has passed since I needed this, and going forward, needing such control in an iOS6 and later context, your suggestion Jon, using the new delegate method is exactly the right answer. I'm going to remove the iOS5 tag on this question. You should then answer it formally, which I'll accept.idStar

3 Answers

3
votes

In iOS 6.0 and later, you can prevent a segue from firing after the action that would normally trigger it has occurred by overriding the -[UIViewController shouldPerformSegueWithIdentifier:sender:] method and returning NO.

1
votes

You could do a programatic segue where instead using a segue from the controller itself and then you do the segue using performSegueWithIdentifier and then you could test for this particular segue and decide to do it or not.

0
votes

You could subclass UIStoryboardSegue as described in its reference.

A possible implementation:

.h

@interface MYPushNoAnimationSegue : UIStoryboardSegue
@end

.m

#import "MYPushNoAnimationSegue.h"

@implementation MYPushNoAnimationSegue

- (void)perform {
    if (condition) {
        [[(UIViewController *)self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:NO];
    }
}

@end

That being done, you can select your existing segue in the storyboard, then set its style to custom, and enter MYPushNoAnimationSegue to the Segue Class in the Attributes inspector.

I know a lot of time passed, maybe you already solved it, but it might help others who face the same problem.