how can I dismiss keyboard on Enter keypress

北战南征 提交于 2019-12-19 10:24:50

问题


I want to dismiss my keyboard as I press RETURN key.

I have tried by putting button in view's back side.

But how can I do this by pressing RETURN key?


回答1:


-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

Don't forget to add the delegate UITextFieldDelegate




回答2:


I hope you have done UIViewController <UITextFieldDelegate> and yourTextField.delegate=self;

and then in the delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{

   [textField resignFirstResponder];
   return YES;

}



回答3:


I'm presuming you're talking about a UITextField rather than a UITextView as your question isn't that clear? If so then ensure your class is marked as a UITextFieldDelegate in the interface file,

@interface MyController: UIViewController <UITextFieldDelegate> {
   UITextField *activeTextField;

   // ...remainder of code not show ...
}

and then you should implement the two delegate methods as below,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    activeTextField = textField;!    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    activeTextField = nil; 
    [textField resignFirstResponder];
    return YES;
}

However if you're using a UITextView then things are a bit more complicated. The UITextViewDelegate protocol lacks the equivalent to the textFieldShouldReturn: method, presumably since we shouldn’t expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return).

However, there are several ways around the inability of the UITextView to resign as first responder using the keyboard. The usual method is to place a Done button in the navigation bar when the UITextView presents the pop-up keyboard. When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard.

However, depending on how you’ve planned out your interface, you might want the UITextView to resign when the user taps outside the UITextView itself. To do this, you’d subclass UIView to accept touches, and then instruct the text view to resign when the user taps outside the view itself.

Create a new class,

#import <UIKit/UIKit.h>

@interface CustomView : UIView {
   IBOutlet UITextView *textView;
}

@end

Then, in the implementation, implement the touchesEnded:withEvent: method and ask the UITextView to resign as first responder.

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void) awakeFromNib {
    self.multipleTouchEnabled = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began count %d, %@", [touches count], touches);

    [textView resignFirstResponder];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

Once you’ve added the class, you need to save all your changes, then go into Interface Builder and click on your view. Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your CustomView rather than the default UIView class. Then in the Connections Inspector, drag the textView outlet to the UITextView. After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. Note however that if the UIView you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. So while this solution is elegant, it can be used in only some situations. In many cases, you’ll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard.




回答4:


Make sure your view controller class is a delegate for your UITextField and then use the delegate method in that class:

#pragma mark - Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // Dismiss the keyboard when the Return key is pressed.
    [textField resignFirstResponder];

    return YES;
}


来源:https://stackoverflow.com/questions/9375969/how-can-i-dismiss-keyboard-on-enter-keypress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!