问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/12083415/inhibit-storyboard-segue-from-performing-segue-action