标签:uiview tap set self __weak note res board UI
- (void)setupForDismissKeyboard
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTagGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnyWhereToDismissKeyboard:)];
__weak UIViewController *weakSelf = self;
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note){[weakSelf.view addGestureRecognizer:singleTapGR];}];
[nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note){[weakSelf.view removeGestureRecognizer:singleTapGR];}];
}
- (void)tapAnyWhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer
{
//此method 会将self.view里所有的subview的first responder 都resign掉
[self.view endEditing:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];
}
标签:uiview tap set self __weak note res board UI
原文地址:http://www.cnblogs.com/Myzking/p/7111815.html