2

I learned how to dismiss keyboard when the return key is clicked, or when user touches outside of textfield.

One way is to delegate a class to manage textfield and write the code:

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; //I'm not sure whether I choose YES or NO
}

Another way is like following:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touchToDismissKeyboard.phase == UITouchPhaseBegan) {
    [self.firstTextField resignFirstResponder];
    }
}

However, with either of the methods above, I found other interfaces are "enabled", such as segmented control or switch which are on the same view as textfield. In addition, when these enabled interfaces is clicked, the keyboard is still on the screen.

Then, my question are how I can make other interfaces unavailable ("enabled = NO") when keyboard appears, and how I can dismiss keyboard with touch on anywhere out of keyboard or textfield without changing values of other interfaces.

  • possible duplicate of [iphone, dismiss keyboard when touching outside of textfield](http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield) – Sunny Shah Jul 02 '14 at 13:38
  • I'm sorry I wouldn't be able to explain my situation enough exactly. My question is how I can make other interfaces than keyboard unclickable while editing textfield (while keyboard appears), which I think is different problem from the question your suggested. Anyway, finally I found a way to solve my problem, but Thank you for commenting and I'll keep it mind to search Q&A which may help me before my questioning as ever. –  Jul 03 '14 at 08:20

1 Answers1

0

Use Tap gesture recognizer

UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmissKeyBoard)];
tapRecognize.delegate = self;
tapRecognize.numberOfTapsRequired = 1;
[self.viewB addGestureRecognizer:tapRecognize];

Then dissmissKeyBoard method implementation.

- (void)dissmissKeyBoard
{
    [yourTextField resignFirstResponder];
}

Hope this will help you.

  • Thank you for commenting. But, with your code, there were still the fact that other interfaces than keyboard were enabled, although I want those to be unavailable. After hours of working, I found the way that is to write the code to make interfaces unavailable in "- (void)textFieldDidBeginEditing" method, which was successful. Anyway, sorry for my vague question, and thank you again. –  Jul 03 '14 at 08:05