如何在访问其他文本字段时使其他文本字段的键盘消失

时间:2012-03-20 05:51:28

标签: iphone objective-c ios uitextfield uikeyboard

我的视图中有两个文本字段。我是用IB做的。在第二个文本字段中我正在使用操作表

在textField1中输入文本后,我在文本Field-2中。在第二个文本字段中,我使用带有选择器的操作表来选择日期。所以我在打开动作表之前辞去了textfield -2键盘。当我试图辞职键盘时,我解除了操作表后,它没有返回。

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

辞去textfield1 ..

4 个答案:

答案 0 :(得分:4)

- (void) viewDidLoad
{
   //don't forget to add <UITextFieldDelegate> in your .h
   firstTextField.delegate=self;
   secondTextField.delegate=self;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField.tag==2)
    {
      //Here you can call function to view your datepicker
      return NO; //Will not open keyboard for second textfield.
    }
    return YES;
}

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

答案 1 :(得分:0)

你有返回当前textField textFieldShouldReturn方法改变你的textFieldShouldReturn metod如下所示

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

答案 2 :(得分:0)

将标记设置为每个文本字段并在

中进行检查
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([textfield.tag == 1])
   {
       [textFieldFirst resignFirstResponder];
   }
   else
   {
     // your action sheet code here
   }
   return YES;
}

答案 3 :(得分:0)

您需要为TextField提供标记,

在此方法中,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{              
    if (textField.tag == 2)
    {
       [textField1 resignFirstResponder];
       // your action sheet code here 
    } 
}

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