Answers:
首先,您需要UITextFieldDelegate
像这样在View / ViewController的头文件中遵守协议:
@interface YourViewController : UIViewController <UITextFieldDelegate>
然后,在您的.m文件中,您需要实现以下UITextFieldDelegate
协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];
确保关闭键盘。
在.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
您的UITextFields应该具有一个委托对象(UITextFieldDelegate)。在您的委托中使用以下代码使键盘消失:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
到目前为止应该可以工作...
经过一段时间的思考,找到了有意义的东西,这就是我整理的东西,它就像一种魅力。
。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
希望这可以帮助!
添加它而不是预定义的类
class ViewController: UIViewController, UITextFieldDelegate {
在键盘外单击时卸下键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
并在按Enter时卸下键盘
在viewDidLoad()中添加此行
inputField是使用的textField的名称。
self.inputField.delegate = self
并添加此功能
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
斯威夫特2:
这是每件事要做的事!
关闭键盘Done
按钮或者Touch outSide
,Next
以转到下一个输入。
在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)
}
快速地,您应该委派UITextfieldDelegate,在viewController中,重要的是不要忘记它,例如:
class MyViewController: UITextfieldDelegate{
mytextfield.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}
您可以尝试使用此UITextfield子类,该子类可以设置文本动态更改UIReturnKey的条件:https :
//github.com/codeinteractiveapps/OBReturnKeyTextField