ios 按返回时如何以编程方式关闭键盘iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18755410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dismiss keyboard iOS programmatically when pressing return
提问by noobsmcgoobs
I created a UITextField
programmatically making the UITextField
a property of the viewController. I need to dismiss the keyboard with the return and the touch on the screen. I was able to get the screen touch to dismiss, but pressing return is not working.
我以UITextField
编程方式创建了UITextField
一个视图控制器的属性。我需要通过返回和触摸屏幕来关闭键盘。我能够让屏幕触摸解除,但按返回不起作用。
I've seen how to do it with storyboards and by allocating and initializing the UITextField
object directly without creating it as a property. Possible to do?
我已经看到如何使用情节提要以及通过UITextField
直接分配和初始化对象而不将其创建为属性来做到这一点。有可能吗?
.h
。H
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, atomic) UITextField *username;
@end
.m
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor blueColor];
self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)];
self.username.placeholder = @"Enter your username";
self.username.backgroundColor = [UIColor whiteColor];
self.username.borderStyle = UITextBorderStyleRoundedRect;
if (self.username.placeholder != nil) {
self.username.clearsOnBeginEditing = NO;
}
_username.delegate = self;
[self.view addSubview:self.username];
[_username resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
@end
回答by Nitin Gohel
The simple way is to connect the delegate of UITextField
to self (self.mytestField.delegate = self
) and dismiss the keyboard in the method textFieldShouldReturn
using [textField resignFirstResponder];
简单的方法是将 的委托连接UITextField
到 self ( self.mytestField.delegate = self
) 并在textFieldShouldReturn
使用的方法中关闭键盘[textField resignFirstResponder];
Another way to dismiss the keyboard is the following:
另一种关闭键盘的方法如下:
[self.view endEditing:YES];
Put [self.view endEditing:YES];
where you would like to dismiss the keyboard (Button event, Touch event, etc.).
放置[self.view endEditing:YES];
在您想要关闭键盘的位置(按钮事件、触摸事件等)。
回答by iPatel
Add a delegate method of UITextField
like this:
添加一个这样的委托方法UITextField
:
@interface MyController : UIViewController <UITextFieldDelegate>
And set your textField.delegate = self;
then also add two delegate methods of UITextField
并设置您textField.delegate = self;
然后还添加两个委托方法UITextField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
// It is important for you to hide the keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
回答by Vijay Dokrimare
//Hide keyBoard by touching background in view
//通过在视图中触摸背景隐藏键盘
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
回答by Chathuranga Silva
simply use this in swift to dismiss keyboard:
只需在 swift 中使用它即可关闭键盘:
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
Swift 3
斯威夫特 3
UIApplication.shared.sendAction(#selector(UIResponder.resign??FirstResponder), to: nil, from: nil, for: nil)
回答by Sam
SWIFT 4:
快速 4:
self.view.endEditing(true)
or
或者
Set text field's delegate to current viewcontroller and then:
将文本字段的委托设置为当前视图控制器,然后:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Objective-C:
目标-C:
[self.view endEditing:YES];
or
或者
Set text field's delegate to current viewcontroller and then:
将文本字段的委托设置为当前视图控制器,然后:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
回答by DKong
In the App Delegate, you can write
在 App Delegate 中,您可以编写
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.window endEditing:YES];
}
use this way, you can don`t write too much code.
使用这种方式,就可以不用写太多代码了。
回答by Zen
Try to get an idea about what a first responder is in iOS view hierarchy. When your textfield becomes active(or first responder) when you touch inside it (or pass it the messasge becomeFirstResponder
programmatically), it presents the keyboard. So to remove your textfield from being the first responder, you should pass the message resignFirstResponder
to it there.
尝试了解 iOS 视图层次结构中的第一响应者是什么。当您触摸文本字段(或以becomeFirstResponder
编程方式将消息传递给它)时,文本字段变为活动状态(或第一响应者)时,它会显示键盘。因此,要将您的文本字段从第一响应者中删除,您应该将消息传递resignFirstResponder
给它。
[textField resignFirstResponder];
And to hide the keyboard on its return button, you should implement its delegate method textFieldShouldReturn:
and pass the resignFirstResponder
message.
并且要隐藏其返回按钮上的键盘,您应该实现其委托方法textFieldShouldReturn:
并传递resignFirstResponder
消息。
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
回答by Takide
Here's what I use in my code. It works like a charm!
这是我在代码中使用的内容。它就像一个魅力!
In yourviewcontroller.h add:
在 yourviewcontroller.h 添加:
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
现在在 .m 文件中,将其添加到您的 ViewDidLoad 函数中:
- (void)viewDidLoad {
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}
Also, add this function in the .m file:
此外,在 .m 文件中添加此函数:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
回答by Matias Elorriaga
For a group of UITextView
s inside a ViewController
:
对于 a 中的一组UITextView
s ViewController
:
Swift 3.0
斯威夫特 3.0
for view in view.subviews {
if view is UITextField {
view.resignFirstResponder()
}
}
Objective-C
目标-C
// hide keyboard before dismiss
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
// no need to cast
[view resignFirstResponder];
}
}
回答by Matias Elorriaga
To dismiss a keyboard after the keyboard has popped up, there are 2 cases,
在键盘弹出后关闭键盘,有两种情况,
when the UITextField is inside a UIScrollView
when the UITextField is outside a UIScrollView
当 UITextField位于 UIScrollView 内时
当 UITextField在 UIScrollView 之外时
2.when the UITextField is outside a UIScrollView override the method in your UIViewController subclass
2.当 UITextField 在 UIScrollView 之外时覆盖 UIViewController 子类中的方法
you must also add delegate for all UITextView
您还必须为所有 UITextView 添加委托
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- In a scroll view, Tapping outside will not fire any event, so in that case use a Tap Gesture Recognizer, Drag and drop a UITapGesturefor the scroll view and create an IBAction for it.
- 在滚动视图中,点击外部不会触发任何事件,因此在这种情况下,请使用点击手势识别器,为滚动视图拖放UITapGesture并为其创建 IBAction。
to create a IBAction, press ctrl+ click the UITapGesture and drag it to the .h file of viewcontroller.
要创建一个IBAction,按ctrl+单击UITapGesture 并将其拖到viewcontroller 的.h 文件中。
Here I have named tappedEvent as my action name
在这里,我将 tappedEvent 命名为我的操作名称
- (IBAction)tappedEvent:(id)sender {
[self.view endEditing:YES]; }
the abouve given Information was derived from the following link, please refer for more information or contact me if you dont understand the abouve data.
以上信息来源于以下链接,如果您不理解以上数据,请参考以获取更多信息或联系我。
http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/
http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/