UITextField becomeFirstResponder 在 UIControlEventEditingDidEndOnExit 中不起作用
UITextField becomeFirstResponder does not work in UIControlEventEditingDidEndOnExit
我有一个 UITextField
和 UILabel
。当用户在 textField 中输入一些文本并点击键盘上的 "Enter" 时,我想将文本保存到标签然后清除 textField 并再次使其成为第一响应者。但是出了点问题。 becomeFirstResponder 不起作用。
这里是ViewController的代码:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.textField addTarget:self action:@selector(saveResult:)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
-(void) saveResult: (id) sender {
self.label.text = self.textField.text;
self.textField.text = @"";
[self.textField becomeFirstResponder];
}
@end
在.h文件和.m文件中设置UITextField的Delegate和Datasource然后试试
希望对您有所帮助。
如果您不再需要目标。在选择器方法中删除目标,它工作正常。
-(void) saveResult: (id) sender {
self.label.text = self.textField.text;
self.textField.text = @"";
[self.textField removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
[self.textField becomeFirstResponder];
}
给你,这是一个工作示例,"setFrame" 方法是突然组成的,你可以更改它或只使用为你完成所有这些的委托方法,将其弹出到您的代码中,加载 iphone 6+ 模拟器,选择显示硬件键盘,这将起作用。没有理由向您的 UITextField 添加触摸事件处理程序,因为 Apple 提供的委托方法会为您完成所有工作。
@interface NSHHomeViewController () <UITextFieldDelegate>
@end
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
{
UITextField * _passwordField;
UILabel * hid;
}
- (void)viewDidLoad {
[super viewDidLoad];
hid = [UILabel new];
[hid setFrame:CGRectMake(80, 200, 300, 60)];
[hid setFont:fontMagicForBoldNavigationBarNSHFont];
[hid setBackgroundColor:[UIColor babyBlue]];
[hid setTextColor:[UIColor blackBean]];
[self.view addSubview:hid];
_passwordField = [[UITextField alloc] init];
[_passwordField setSecureTextEntry:TRUE];
[_passwordField setPlaceholder:@"Password"];
[_passwordField setReturnKeyType:UIReturnKeyGo];
[_passwordField setTextAlignment:NSTextAlignmentLeft];
[_passwordField setDelegate:self];
[_passwordField setFont:fontMagicForRegularNSHFont];
[_passwordField setEnablesReturnKeyAutomatically:true];
[_passwordField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[_passwordField setFrame:CGRectMake(30, SCREEN_HEIGHT/2, SCREEN_WIDTH-60, 55)];
UIView *spacerView4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[_passwordField setLeftViewMode:UITextFieldViewModeAlways];
[_passwordField setLeftView:spacerView4];
[_passwordField setTextColor:[UIColor NSHBlueColor]];
[_passwordField setValue:fontMagicForRegularNSHFont forKeyPath:@"_placeholderLabel.font"];
[_passwordField setClearButtonMode:UITextFieldViewModeAlways];
[_passwordField setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
[self.view addSubview:_passwordField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _passwordField) {
[hid setText:_passwordField.text];
[_passwordField setText:@""];
[_passwordField resignFirstResponder];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_passwordField becomeFirstResponder];
});
}
return TRUE;
}
这只是一个工作示例,您需要正确声明委托并正确声明 UITextField 的委托,如上所示。然后为键盘上的按钮选择你想要的 return 键类型,然后你还可以使用 UITextFieldDelegate 的 "texFieldShouldReturn" 拦截这些事件,你不必调用定时调度队列,但它把这里面的效果是贴到标签上,清除文本,退出键盘,然后在 1/2 秒后,键盘再次成为第一响应者。
我有一个 UITextField
和 UILabel
。当用户在 textField 中输入一些文本并点击键盘上的 "Enter" 时,我想将文本保存到标签然后清除 textField 并再次使其成为第一响应者。但是出了点问题。 becomeFirstResponder 不起作用。
这里是ViewController的代码:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.textField addTarget:self action:@selector(saveResult:)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
-(void) saveResult: (id) sender {
self.label.text = self.textField.text;
self.textField.text = @"";
[self.textField becomeFirstResponder];
}
@end
在.h文件和.m文件中设置UITextField的Delegate和Datasource然后试试
希望对您有所帮助。
如果您不再需要目标。在选择器方法中删除目标,它工作正常。
-(void) saveResult: (id) sender {
self.label.text = self.textField.text;
self.textField.text = @"";
[self.textField removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
[self.textField becomeFirstResponder];
}
给你,这是一个工作示例,"setFrame" 方法是突然组成的,你可以更改它或只使用为你完成所有这些的委托方法,将其弹出到您的代码中,加载 iphone 6+ 模拟器,选择显示硬件键盘,这将起作用。没有理由向您的 UITextField 添加触摸事件处理程序,因为 Apple 提供的委托方法会为您完成所有工作。
@interface NSHHomeViewController () <UITextFieldDelegate>
@end
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
{
UITextField * _passwordField;
UILabel * hid;
}
- (void)viewDidLoad {
[super viewDidLoad];
hid = [UILabel new];
[hid setFrame:CGRectMake(80, 200, 300, 60)];
[hid setFont:fontMagicForBoldNavigationBarNSHFont];
[hid setBackgroundColor:[UIColor babyBlue]];
[hid setTextColor:[UIColor blackBean]];
[self.view addSubview:hid];
_passwordField = [[UITextField alloc] init];
[_passwordField setSecureTextEntry:TRUE];
[_passwordField setPlaceholder:@"Password"];
[_passwordField setReturnKeyType:UIReturnKeyGo];
[_passwordField setTextAlignment:NSTextAlignmentLeft];
[_passwordField setDelegate:self];
[_passwordField setFont:fontMagicForRegularNSHFont];
[_passwordField setEnablesReturnKeyAutomatically:true];
[_passwordField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[_passwordField setFrame:CGRectMake(30, SCREEN_HEIGHT/2, SCREEN_WIDTH-60, 55)];
UIView *spacerView4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[_passwordField setLeftViewMode:UITextFieldViewModeAlways];
[_passwordField setLeftView:spacerView4];
[_passwordField setTextColor:[UIColor NSHBlueColor]];
[_passwordField setValue:fontMagicForRegularNSHFont forKeyPath:@"_placeholderLabel.font"];
[_passwordField setClearButtonMode:UITextFieldViewModeAlways];
[_passwordField setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
[self.view addSubview:_passwordField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _passwordField) {
[hid setText:_passwordField.text];
[_passwordField setText:@""];
[_passwordField resignFirstResponder];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_passwordField becomeFirstResponder];
});
}
return TRUE;
}
这只是一个工作示例,您需要正确声明委托并正确声明 UITextField 的委托,如上所示。然后为键盘上的按钮选择你想要的 return 键类型,然后你还可以使用 UITextFieldDelegate 的 "texFieldShouldReturn" 拦截这些事件,你不必调用定时调度队列,但它把这里面的效果是贴到标签上,清除文本,退出键盘,然后在 1/2 秒后,键盘再次成为第一响应者。