我有两个UITextFields
(例如用户名和密码),但是当按下键盘上的返回键时我无法摆脱键盘。我怎样才能做到这一点?
答案 0 :(得分:241)
首先,您需要在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
答案 1 :(得分:19)
实现UITextFieldDelegate方法,如下所示:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
答案 2 :(得分:8)
有关此主题的完整讨论,请参阅Managing the Keyboard。
答案 3 :(得分:6)
您的UITextField应该有一个委托对象(UITextFieldDelegate)。在委托中使用以下代码使键盘消失:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
到目前为止应该工作......
答案 4 :(得分:6)
我做了几次试验,有同样的问题,这对我有用:
在 -
检查您的拼写(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
我在textField
而不是textfield
纠正了我,将大写“F”......和宾果游戏!它工作..
答案 5 :(得分:4)
按下返回键后,请致电:
[uitextfield resignFirstResponder];
答案 6 :(得分:3)
经过相当多的时间寻找有意义的东西,这就是我放在一起的东西,它就像一个魅力。
·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
希望这有帮助!
答案 7 :(得分:3)
将UITextField的Delegate设置为ViewController,在File的Owner和UITextField之间添加一个引用插座,然后实现此方法:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == yourTextField)
{
[textField resignFirstResponder];
}
return NO;
}
答案 8 :(得分:3)
添加此而不是预定义的类
class ViewController: UIViewController, UITextFieldDelegate {
在键盘外单击时删除键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
并在按下后删除键盘
在viewDidLoad()
中添加此行inputField是使用的textField的名称。
self.inputField.delegate = self
并添加此功能
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
答案 9 :(得分:2)
在swift中,你应该委托UITextfieldDelegate ,重要的是不要忘记它,在viewController中,如:
class MyViewController: UITextfieldDelegate{
mytextfield.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}
答案 10 :(得分:1)
如果您想在警告框textfileds上书写时消失键盘
[[alertController.textFields objectAtIndex:1] resignFirstResponder];
答案 11 :(得分:0)
您可以向uiTextField添加IBAction(相关事件是“退出时结束”),IBAction可以命名为hideKeyboard,
-(IBAction)hideKeyboard:(id)sender
{
[uitextfield resignFirstResponder];
}
另外,您可以将其应用于其他textFields或按钮,例如,当您单击它以隐藏键盘时,您可以向视图添加隐藏按钮。
答案 12 :(得分:0)
您可以尝试使用此UITextfield子类,您可以为文本设置条件以动态更改UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField