你怎么解雇时的键盘编辑UITextField
-
07-07-2019 - |
题
我知道,我需要告诉我的UITextField辞职的第一响应者的时候我想dismis的键盘,但是我不知道该怎么知道当户已经按下"完成"关键的键盘上。有没有通知我可以看吗?
解决方案
我将 UITextField
的委托设置为我的 ViewController
类。
在该课程中,我实现了以下方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
其他提示
如果将文本字段的DidEndOnExit事件连接到InterfaceBuilder中的操作(IBAction),则当用户解除键盘(使用返回键)时将发送消息,并且发送方将是对触发的UITextField的引用事件。
例如:
-(IBAction)userDoneEnteringText:(id)sender
{
UITextField theField = (UITextField*)sender;
// do whatever you want with this text field
}
然后,在InterfaceBuilder中,将文本字段的DidEndOnExit事件链接到控制器上的此操作(或者用于链接UI中的事件的任何操作)。每当用户输入文本并关闭文本字段时,控制器将发送此消息。
您还可以在控制器中创建方法
-(IBAction)editingEnded:(id)sender{
[sender resignFirstResponder];
}
然后在IB连接事件中的连接检查器中“已退出时结束”它。
UITextField
的 delegate
设置为等于文本字段所在的ViewController。无论何时何地,您都可以这样做。我选择了 viewDidLoad
方法。
- (void)viewDidLoad
{
// sets the textField delegates to equal this viewController ... this allows for the keyboard to disappear after pressing done
daTextField.delegate = self;
}
这是获取自动键盘解雇行为的技巧,完全没有代码。在nib中,在Identity检查器中编辑First Responder代理对象,添加新的第一响应者操作;我们称之为 dummy:
。现在将文本字段的Did End on Exit事件挂钩到First Responder代理对象的 dummy:
操作。而已!由于文本字段的“退出时结束”事件现在具有操作–目标对,因此当用户点击Return时,文本字段会自动关闭其键盘;因为没有为响应者链发送的消息找到处理程序没有惩罚,即使在任何地方都没有 dummy:
的实现,应用程序也不会崩溃。
添加
[textField endEditing:YES];
您要禁用键盘并显示选择器视图。
在Swift中,您可以在Controller中编写IBAction,并将文本字段的已结束退出事件设置为该操作
@IBAction func returnPressed(sender: UITextField) {
self.view.endEditing(true)
}
您也可以使用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.yourTextField resignFirstResponder];
}
如果你有很多Uitextfields,最好的一个:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
这是我必须做的才能让它发挥作用,而且我觉得对于键盘的数字键盘(或没有完成键的任何其他键盘)的人来说是必要的:
- 我将ViewController中的UIView更改为UIControl。
-
我创建了一个名为
的函数-(IBAction)backgroundIsTapped:(id)sender
醇>
这也在.h文件中定义。
在此之后,我链接到Interface Builder中的ViewController的'touch down'位。
在'背景被点击'功能中,我把它放在:
[answerField resignFirstResponder];
请记住将'answerField'更改为要从中移除键盘的UITextField的名称(显然要确保您的UITextField定义如下:)
IBOutlet UITextField * <nameoftextfieldhere>;
我知道这个话题可能很久以前就已经死了......但是我希望这会对某人有所帮助!
您会注意到方法“textFieldShouldReturn”提供已达到DONE键的文本字段对象。如果设置TAG,则可以打开该文本字段。或者,您可以跟踪对象的指针并将其与其创建者存储的某个成员值进行比较。
我的方法就像这样自学:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%s", __FUNCTION__);
bool fDidResign = [textField resignFirstResponder];
NSLog(@"%s: did %resign the keyboard", __FUNCTION__, fDidResign ? @"" : @"not ");
return fDidResign;
}
与此同时,我把“验证”放在了拒绝辞职的测试如下。它仅用于说明,因此如果用户键入NO!进入该领域,它不会被解雇。行为是我想要的,但输出的顺序并不像我预期的那样。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"%s", __FUNCTION__);
if( [[textField text] isEqualToString:@"NO!"] ) {
NSLog(@"%@", textField.text);
return NO;
} else {
return YES;
}
}
以下是我拒绝接受的NSLog输出。你会注意到我正在返回辞职的结果,但是我希望它返回FALSE给我报告给来电者?!除此之外,它具有必要的行为。
13.313 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:] 13.320 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:] 13.327 StudyKbd[109:207] NO! 13.333 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard 59.891 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:] 59.897 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:] 59.917 StudyKbd[109:207] -[StudyKbdViewController doneEditText]: NO 59.928 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard
这是一个相当清洁的方式来结束版与返回的关键或触的背景。
在接口的建设者,嵌入你的领域中的看类UIFormView
这是什么类:
- 自动连接本身作为领域的代表(或者醒了从一笔尖或加入手)
- 保持一个参考的前编辑的领域
- 驳回上的键盘的回报,或者触摸的背景
这里是代码:
接口
#import <UIKit/UIKit.h>
@interface UIFormView : UIView<UITextFieldDelegate>
-(BOOL)textFieldValueIsValid:(UITextField*)textField;
-(void)endEdit;
@end
执行情况
#import "UIFormView.h"
@implementation UIFormView
{
UITextField* currentEditingTextField;
}
// Automatically register fields
-(void)addSubview:(UIView *)view
{
[super addSubview:view];
if ([view isKindOfClass:[UITextField class]]) {
if ( ![(UITextField*)view delegate] ) [(UITextField*)view setDelegate:self];
}
}
// UITextField Protocol
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
currentEditingTextField = textField;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
currentEditingTextField = NULL;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self endEdit];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([self textFieldValueIsValid:textField]) {
[self endEdit];
return YES;
} else {
return NO;
}
}
// Own functions
-(void)endEdit
{
if (currentEditingTextField) {
[currentEditingTextField endEditing:YES];
currentEditingTextField = NULL;
}
}
// Override this in your subclass to handle eventual values that may prevent validation.
-(BOOL)textFieldValueIsValid:(UITextField*)textField
{
return YES;
}
@end
通过继承的形式,压倒一切的
textFieldValueIsValid:
方法,您 可以避免最终的版本,如果价值是不正确的,对于给定 领域。如果一个领域具有代表设置在接口的建设者,然后形式没有改变它。我没有看到许多原因,设定一个不同的委派到一个特定的领域,但是,为什么不...
有许多方法来改善这种形式视类附上委托,做一些布局,处理当的键盘涵盖领域(用currentEditingTextField框架),自动启动版的下一个领域,...
我亲自把它放在我的框架,并始终子类,它添加功能,它是经常有用的"因"。
我希望这将有所帮助。所有的欢呼声
如果你想在UIViewController中进行所有编辑,你可以使用。
[[self view]endEditing:YES];
如果您想要关闭一个特定的UITextField键盘,请使用。
1.在viewcontroller.h中添加委托
<UITextFieldDelegate>
-
使委派无法进入您的文本域。
self.yourTextField.delegate = self;
-
将此方法添加到viewcontroller中。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { [textField resignFirstResponder]; 返回YES;}
醇>
以编程方式在swift 3
中设置UITextField的委托在ViewController.Swift文件中实现UITextFieldDelegate(例如,类ViewController:UIViewController,UITextFieldDelegate {)
lazy var firstNameTF: UITextField = {
let firstname = UITextField()
firstname.placeholder = "FirstName"
firstname.frame = CGRect(x:38, y: 100, width: 244, height: 30)
firstname.textAlignment = .center
firstname.borderStyle = UITextBorderStyle.roundedRect
firstname.keyboardType = UIKeyboardType.default
firstname.delegate = self
return firstname
}()
lazy var lastNameTF: UITextField = {
let lastname = UITextField()
lastname.placeholder = "LastName"
lastname.frame = CGRect(x:38, y: 150, width: 244, height: 30)
lastname.textAlignment = .center
lastname.borderStyle = UITextBorderStyle.roundedRect
lastname.keyboardType = UIKeyboardType.default
lastname.delegate = self
return lastname
}()
lazy var emailIdTF: UITextField = {
let emailid = UITextField()
emailid.placeholder = "EmailId"
emailid.frame = CGRect(x:38, y: 200, width: 244, height: 30)
emailid.textAlignment = .center
emailid.borderStyle = UITextBorderStyle.roundedRect
emailid.keyboardType = UIKeyboardType.default
emailid.delegate = self
return emailid
}()
// Mark: - 处理委托textField ..
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == firstNameTF {
lastNameTF.becomeFirstResponder()
}
else if textField == lastNameTF {
emailIdTF.becomeFirstResponder()
}
else {
view.emailIdTF(true)
}
return true
}
创建一个函数hidekeyboard并将其链接到.xib文件中的textfield并选择DidEndOnExit
-(IBAction)Hidekeyboard
{
textfield_name.resignFirstResponder;
}
如果您使用Interface Builder创建了视图,请使用以下命令 只需创建一个方法,
-(IBAction)dismissKeyboard:(id)sender
{
[sender resignFirstResponder];
}
只需右键单击视图中的文本字段,然后将事件设置为“退出时已结束”,并将其连接到方法“dismissKeyboard”。
初学者的最佳指南是 “Head First iPhone and iPad Development,2nd Edition”
试试这个
- (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
//====================================================
// textFieldShouldReturn:
//====================================================
-(BOOL) textFieldShouldReturn:(UITextField*) textField {
[textField resignFirstResponder];
if(textField.returnKeyType != UIReturnKeyDone){
[[textField.superview viewWithTag: self.nextTextField] becomeFirstResponder];
}
return YES;
}
任何人都在寻找 Swift 3
1)确保 UITextField的代理连接到故事板中的ViewController
2)在ViewController.Swift文件中实现 UITextFieldDelegate (例如,类ViewController:UIViewController,UITextFieldDelegate {)
3)使用下面的委托方法
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false }
这就是我在Swift 4.2中解雇键盘的方式,它对我有用:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action:
#selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
numberField.resignFirstResponder()
}
Swift 3,iOS 9 +
@IBAction private func noteFieldDidEndOnExit(_ sender:UITextField){}
textField.returnKeyType = UIReturnKeyDone;