双击需要键盘而不是单击

时间:2014-02-25 09:09:50

标签: ios iphone ios5 ios6 ios7

我不想在文本字段上单击显示键盘,而是想在双击时显示它。 为此我创建了两个动作,一个用于单击,另一个用于双击,也用于文本域委托方法:

            [textField1 addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchDownRepeat];   // for double tap
            [textField1 addTarget:self action:@selector(clicked1) forControlEvents:UIControlEventTouchDown];   // for single tap

METHOD DEFINATION:

-(void)clicked    // action for double tap
{
    count=2;
     NSLog(@"--------double------%d",count);
    [textField1 becomeFirstResponder];
}

-(void)clicked1   //action for single tap

{
    count=1;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField  //textfield delegate
{
if(count==1)
    return NO;
else
    return YES;
}

此代码工作正常,但并非总是如此。有时未检测到“UIControlEventTouchDownRepeat”事件,并且“单击”功能不起作用。 我无法理解为什么事件每次都不起作用的原因。

2 个答案:

答案 0 :(得分:1)

嗨,你可以试试这个。

//Tap GestureRecognizer to recognize tap
     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(youMethodName)];
        doubleTap.numberOfTapsRequired = 2; 
        [self.view addGestureRecognizer:doubleTap];

//使用yourMethodName显示键盘

答案 1 :(得分:0)

不要为textfield添加任何操作 如下所示

   @interface ViewController ()<UIScrollViewDelegate,UITextFieldDelegate>
    {
      int count;
    }

 - (void)viewDidLoad
   {
     count = 0;//initially put zero
     self.textField.delegate = self; //textfield added in xib or story board

   }

   - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  {
      count++;
      if(count == 2)
      {
        count = 0;
        return YES;
      }

     if(count >= 2)
     count = 0;
     return NO;

 }

 //for testing i am doing like this
 - (BOOL)textFieldShouldReturn:(UITextField *)textField
  {
    [textField resignFirstResponder];
    return YES;
  }