ios 如何使iPhone上的返回键使键盘消失?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6190276/
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 make return key on iPhone make keyboard disappear?
提问by K.Honda
I have two UITextFields(e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?
我有两个UITextFields(例如用户名和密码),但是按键盘上的返回键时我无法摆脱键盘。我怎样才能做到这一点?
回答by Sid
First you need to conform to the UITextFieldDelegateProtocol in your View/ViewController's header file like this:
首先,您需要UITextFieldDelegate在 View/ViewController 的头文件中遵守协议,如下所示:
@interface YourViewController : UIViewController <UITextFieldDelegate>
Then in your .m file you need to implement the following UITextFieldDelegateprotocol method:
然后在您的 .m 文件中,您需要实现以下UITextFieldDelegate协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];makes sure the keyboard is dismissed.
[textField resignFirstResponder];确保关闭键盘。
Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:
在 .m 中初始化文本字段后,确保将视图/视图控制器设置为 UITextField 的委托:
yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on
回答by Nick Weaver
Implement the UITextFieldDelegate method like this:
像这样实现 UITextFieldDelegate 方法:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
回答by Caleb
See Managing the Keyboardfor a complete discussion on this topic.
有关此主题的完整讨论,请参阅管理键盘。
回答by cschwarz
Your UITextFields should have a delegate object (UITextFieldDelegate). Use the following code in your delegate to make the keyboard disappear:
你的 UITextFields 应该有一个委托对象 (UITextFieldDelegate)。在您的委托中使用以下代码使键盘消失:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
Should work so far...
到目前为止应该工作......
回答by Ema
Took me couple trials, had same issue, this worked for me:
花了我几次试验,遇到了同样的问题,这对我有用:
Check your spelling at -
检查您的拼写 -
(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
I corrected mine at textFieldinstead of textfield, capitalise "F"... and bingo!! it worked..
我更正了我的textField而不是textfield,大写“F”......和宾果游戏!!有效..
回答by Conor Taylor
When the return key is pressed, call:
当按下回车键时,调用:
[uitextfield resignFirstResponder];
回答by Ali Kahoot
Add this instead of the pre-defined class
添加这个而不是预定义的类
class ViewController: UIViewController, UITextFieldDelegate {
To remove keyboard when clicked outside the keyboard
在键盘外单击时移除键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
and to remove keyboard when pressed enter
并在按下 Enter 键时移除键盘
add this line in viewDidLoad()
在 viewDidLoad() 中添加这一行
inputField is the name of the textField used.
inputField 是使用的 textField 的名称。
self.inputField.delegate = self
and add this function
并添加此功能
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
回答by user2904476
After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.
经过相当长的时间寻找有意义的东西后,这就是我放在一起的东西,它就像一个魅力。
.h
。H
//
// ViewController.h
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;
@end
.m
.m
//
// ViewController.m
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// _theTextField is the name of the parameter designated in the .h file.
_theTextField.returnKeyType = UIReturnKeyDone;
[_theTextField setDelegate:self];
}
// This part is more dynamic as it closes the keyboard regardless of what text field
// is being used when pressing return.
// You might want to control every single text field separately but that isn't
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
@end
Hope this helps!
希望这可以帮助!
回答by Avinash651
Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:
将 UITextField 的 Delegate 设置为您的 ViewController,在 File's Owner 和 UITextField 之间添加一个引用插座,然后实现此方法:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == yourTextField)
{
[textField resignFirstResponder];
}
return NO;
}
回答by Mojtabye
Swift 2 :
斯威夫特2:
this is what is did to do every thing !
这就是所做的每一件事!
close keyboard with Donebutton or Touch outSide,Nextfor go to next input.
用Done按钮 或关闭键盘Touch outSide,Next转到下一个输入。
First Change TextFiled Return KeyTo Nextin StoryBoard.
首先在 StoryBoard 中将TextFiled 更改Return Key为Next。
override func viewDidLoad() {
txtBillIdentifier.delegate = self
txtBillIdentifier.tag = 1
txtPayIdentifier.delegate = self
txtPayIdentifier.tag = 2
let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
self.view.addGestureRecognizer(tap)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if(textField.returnKeyType == UIReturnKeyType.Default) {
if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
next.becomeFirstResponder()
return false
}
}
textField.resignFirstResponder()
return false
}
func onTouchGesture(){
self.view.endEditing(true)
}

