如何使iPhone上的返回键使键盘消失?

我有两个UITextFields (例如用户名和密码),但当按下键盘上的返回键时,我无法摆脱键盘。 我怎样才能做到这一点?

首先,你需要符合你的View / ViewController头文件中的UITextFieldDelegate协议,如下所示:

 @interface YourViewController : UIViewController <UITextFieldDelegate> 

然后在你的.m文件中,你需要实现下面的UITextFieldDelegate协议方法:

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

[textField resignFirstResponder]; 确保键盘被解除。

确保在将.m文本字段初始化后,将view / viewcontroller设置为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 

像这样实现UITextFieldDelegate方法:

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

请参阅pipe理键盘以获取有关此主题的完整讨论。

你的UITextFields应该有一个委托对象(UITextFieldDelegate)。 在委托中使用以下代码使键盘消失:

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

应该工作到目前为止…

带了我几个试验,有同样的问题,这对我工作:

检查你的拼写 –

 (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; 

我更正textField而不是textfield ,大写“F”…和宾果! 有效..

按下返回键时,请致电:

 [uitextfield resignFirstResponder]; 

经过相当多的时间,find有意义的东西,这就是我放在一起,它像一个魅力。

。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

 // // 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 

希望这可以帮助!

设置UITextField的委托给你的ViewController,在文件的所有者和UITextField之间添加一个引用出口,然后实现这个方法:

 -(BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == yourTextField) { [textField resignFirstResponder]; } return NO; } 

添加这个而不是预定义的类

 class ViewController: UIViewController, UITextFieldDelegate { 

在键盘外部单击时删除键盘

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) } 

并按下键盘时删除input

在viewDidLoad()中添加这一行

inputField是使用的textField的名称。

 self.inputField.delegate = self 

并添加此function

 func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } 

Swift 2:

这是什么做每件事情!

Donebuttonclosures键盘或Touch outSideNext转到下一个input。

首先在StoryBoard上更改TextFiled Return Key

 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) } 

在swift中你应该委托UITextfieldDelegate ,它的重要不要忘记它,在viewController中,像:

 class MyViewController: UITextfieldDelegate{ mytextfield.delegate = self func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() } } 

如果您想在提醒框文字文件上书写时消失键盘

 [[alertController.textFields objectAtIndex:1] resignFirstResponder]; 

您可以添加一个IBAction到uiTextField(相关事件是“Did End On Exit”),IBAction可以命名为hideKeyboard,

 -(IBAction)hideKeyboard:(id)sender { [uitextfield resignFirstResponder]; } 

另外,您可以将其应用到其他textFields或button,例如,您可以添加一个隐藏的button到视图,当您单击它来隐藏键盘。

你可以试试这个UITextfield子类,你可以为这个文本设置一个条件来dynamic地改变UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField