我有两个UITextFields
(例如用户名和密码),但在按下键盘的返回键时,我无法摆脱键盘。我该如何解决?
我有两个UITextFields
(例如用户名和密码),但在按下键盘的返回键时,我无法摆脱键盘。我该如何解决?
首先,在您的视图/视图控制器的头文件中按照以下方式符合 UITextFieldDelegate
协议:
@interface YourViewController : UIViewController <UITextFieldDelegate>
然后在您的.m文件中,您需要实现以下UITextFieldDelegate
协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];
会确保键盘被隐藏。
在 .m 文件中初始化textfield后,确保将你的视图/视图控制器设置为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;
}
您的UITextFields应该有一个委托对象(UITextFieldDelegate)。在您的委托中使用以下代码使键盘消失:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
到目前为止应该可以工作了...
经过几次尝试,我遇到了同样的问题,以下方法对我有用:
检查您的拼写是否正确 -
(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
我在textField
中进行了更正,而不是textfield
,将"F"大写...成功了。
[uitextfield resignFirstResponder];
将UITextField的Delegate设置为您的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)
}
并在按下回车键时移除键盘
在viewDidLoad()方法中添加以下代码:
inputField是所使用的textField的名称。
self.inputField.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
经过一番寻找,我终于整理出了一些有意义的内容,并且它们非常有效。
.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
在Swift中,你应该委托UITextFieldDelegate,这很重要,不要忘记在viewController中像这样:
class MyViewController: UITextfieldDelegate{
mytextfield.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}