废话不多说直接上代码,一个appDelegate的分类方法
#import "AppDelegate.h"
@interface AppDelegate (DismissKeyboard)
/** 开启点击空白处隐藏键盘功能 */
- (void)openTouchOutsideDismissKeyboard;
@end
@implementation AppDelegate (DismissKeyboard)
/** 开启点击空白处隐藏键盘功能 */
- (void)openTouchOutsideDismissKeyboard
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addGesture) name:UIKeyboardDidShowNotification object:nil];
}
- (void)addGesture
{
[self.window addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disappearKeyboard)]];
}
- (void)disappearKeyboard
{
[self.window endEditing:YES];
[self.window removeGestureRecognizer:self.window.gestureRecognizers.lastOb ject];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
- 在AppDelegate.m 中 #import "DismissKeyboard.h"
- 在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
加上[self openTouchOutsideDismissKeyboard];
分类方法
使用calloc分配的函数内存需要手动释放
使用Category建立某类的子类