1. 程式人生 > >//點選螢幕任何地方讓鍵盤消失

//點選螢幕任何地方讓鍵盤消失

//點選螢幕任何地方讓鍵盤消失

- (void)setUpForDismissKeyboard {

NSNotificationCenter *nc = [NSNotificationCenterdefaultCenter];

UITapGestureRecognizer *singleTapGR =

    [[UITapGestureRecognizeralloc] initWithTarget:self

action:@selector(tapAnywhereToDismissKeyboard:)];

NSOperationQueue *mainQuene =[NSOperationQueue

mainQueue];

    [nc addObserverForName:UIKeyboardWillShowNotification

object:nil

queue:mainQuene

usingBlock:^(NSNotification *note){

                    [self.viewaddGestureRecognizer:singleTapGR];

                }];

    [nc addObserverForName:UIKeyboardWillHideNotification

object:nil

queue

:mainQuene

usingBlock:^(NSNotification *note){

                    [self.viewremoveGestureRecognizer:singleTapGR];

                }];

}

//點選螢幕任何地方讓鍵盤消失

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {

//method會將self.view裡所有的subviewfirst responderresign

    [self

.viewendEditing:YES];

}