UITextField becomeFirstResponder在UIControlEventEditingDidEndOnExit

时间:2015-08-14 05:03:32

标签: ios objective-c uitextfield

我有UITextFieldUILabel。当用户在textField中输入一些文本并点击"输入"在键盘上我想将文本保存到标签然后清除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

3 个答案:

答案 0 :(得分:1)

在这里,这是一个工作样本," setFrame"方法只是由蓝色组成,您可以更改它或只是使用代理方法为您完成所有这些,将其弹出到您的代码中,加载iphone 6+模拟器,选择显示硬件键盘和这会奏效。由于Apple提供的委托方法可以为您完成所有工作,因此没有理由为您的UITextField添加触摸事件处理程序。

@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的委托,如上所示。然后为键盘上的按钮选择所需的返回键类型,然后使用UITextFieldDelegate&#34; texFieldShouldReturn&#34;来拦截这些事件,您不必调用定时调度排队,但它把它放在那里,以便发布到标签,清除文本,辞退键盘,然后在1/2秒后,键盘再次成为第一响应者。

答案 1 :(得分:0)

如果你不再想要目标。在选择器方法中删除目标,它工作正常。

-(void) saveResult: (id) sender {
    self.label.text = self.textField.text;
    self.textField.text = @"";
    [self.textField removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.textField becomeFirstResponder];
}

答案 2 :(得分:-1)

在.h文件和.m文件中设置UITextField的委托和数据源,然后尝试。

希望它有所帮助。