English | 中文版
An elegant way to handle pop gesture recogenizer event.
Have you ever had such a problem?
- The screen-edge-pan gesture recognizer is invalid when you custom the
leftNavigationItem
. - When you want to make user confirm whether to popToSuperViewController or not, you should prevent the pop and present a `UIAlertController'.
Now you can implement them with a little bit of code.
AppDelegate.m
#import "AppDelegate.h"
#import "PopEvent.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[PopEvent restorePopGestureRecognizer];
return YES;
}
@end
ViewController1.m
#import "ViewController1.h"
#import "PopEvent.h"
@implementation ViewController1
- (void)viewDidLoad {
[super viewDidLoad];
[self addPopEventSelector:@selector(popEvent) viewController:self];
}
- (void)popEvent {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"PopEvent!" message:@"do something" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
PopEvent is available through CocoaPods.
pod 'PopEvent'
Drag the PopEvent
folder to your project.
PopEvent has two functions. First, #import "PopEvent.h"
.
Add only one line of code into -application: didFinishLaunchingWithOptions:
in AppDelegate
.
Make sure the code runs before the RootViewController's initialization when your app isn't loaded from storyboard.
[PopEvent restorePopGestureRecognizer];
Add only one line of code into the ViewController which will be prevented the pop gesture recognizer.
[self addPopEventSelector:@selector(popEvent) viewController:self];
The first parameter need to be replaced by the event function selector.
You can find detail in the Example.
Welcome to send email to me, or open issue on the repository.
The project follow to the MIT license. Welcome contributions.