如何解雇键盘?

时间:2011-09-30 07:38:46

标签: iphone objective-c ipad

我有三个TextField

用于姓名电话和电子邮件。

选择每个textField键盘将出现

-(void)createdTextField{
    phoneField = [[UITextField alloc]initWithFrame:CGRectMake(225, 306, 90, 31)];
    [phoneField setPlaceholder:REQUIRED];
    [phoneField setBorderStyle:UITextBorderStyleRoundedRect];
    phoneField.delegate = self;
    phoneField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [phoneField setTag:10];
    [self.view addSubview:phoneField];

    [self.view addSubview:phoneField];
    nextPhoneField = [[UITextField alloc]initWithFrame:CGRectMake(325, 306, 142, 31)];
    [nextPhoneField setBorderStyle:UITextBorderStyleRoundedRect];
    nextPhoneField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    nextPhoneField.delegate = self;
    nextPhoneField.keyboardType = UIKeyboardTypeNumberPad;
    [nextPhoneField setTag:11];
    [self.view addSubview:nextPhoneField];



    nameField = [[UITextField alloc] initWithFrame:CGRectMake(225, 265, 242, 31)];
    [nameField setPlaceholder:REQUIRED];
    [nameField setBorderStyle:UITextBorderStyleRoundedRect];
    nameField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
//  [nameField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [nameField setTag:12];
    [self.view addSubview:nameField];

    eMailField = [[UITextField alloc]initWithFrame:CGRectMake(225, 347, 242, 31)];
    [eMailField setPlaceholder:REQUIRED];
    [eMailField setBorderStyle:UITextBorderStyleRoundedRect];
    eMailField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    eMailField.keyboardType = UIKeyboardTypeEmailAddress;
    [eMailField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [eMailField setTag:13];

    [self.view addSubview:eMailField];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

@当在所有四个textField中键入return按钮时关闭键盘我需要关闭键盘。

它只发生在手机上没有剩余的TextField。

4 个答案:

答案 0 :(得分:1)

我看到了您粘贴的代码。 最后两个文本字段没有代理。 我认为这就是原因。

答案 1 :(得分:0)

任何时候你想要为给定字段隐藏键盘只需:

[myField resignFirstResponder];

显然myField是当前活动的字段。

答案 2 :(得分:0)

如何解雇UITextField键盘

[textField resignFirstResponder];

如果您想在用户触摸“返回”按钮时关闭,请使用此项(需要设置委托和协议)

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

答案 3 :(得分:-1)

after creation of urEachTextField


   urEachTextField = [[UITextField alloc] initWithFrame:CGRectMake(225, 265, 242, 31)];
    urEachTextField.delegate=self;

head file .h

    @interface urViewController : UIViewController<UITextFieldDelegate>