【问题标题】:Find out UITextfield's end editing without resigning first responder找出 UITextfield 结束编辑而不辞职第一响应者
【发布时间】:2013-03-14 11:59:21
【问题描述】:

当用户在输入UITextfield时,他停了2秒,光标还在UITextfield,那么我们如何识别这个事件呢?即,我想检查编辑是否结束,而无需从该UITextField 退出第一个响应者。 这样做的方法是什么?

【问题讨论】:

    标签: ios uitextfield user-inactivity


    【解决方案1】:

    是的,我们可以检查!与UITextField 代表,- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    - (void) callMeAfterTwoSeconds {
        NSLog(@"I'll call after two seconds of inactivity!");
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];
        [self performSelector:@selector(callMeAfterTwoSeconds) withObject:nil afterDelay:2.0];
    
        return YES;
    }
    

    当您输入时(从键盘上敲击键),它将取消先前对 callMeAfterTwoSeconds 函数的调用,一旦您停止,它会将其设置为延迟 2 秒后调用,是的,它将在 2 秒后调用.

    更新: 即使您可以将该文本字段作为对象传递给 performSelector 以了解哪个文本字段处于非活动状态,因为您的 callMeAfterTwoSeconds 函数将类似于,

    - (void) callMeAfterTwoSeconds:(UITextField *)textfield {
        if(textfield == txtUserName) {
            NSLog(@"User textfield has NO activity from last two seconds!"); }
    }
    

    【讨论】:

      【解决方案2】:

      转到UITextField 的连接检查器,然后将“已发送事件”列表中的“编辑更改”连接到您选择的预定义IBAction。或者,如果您不使用 Storyboard,您可以通过编程方式进行操作。

      [youTextField addTarget:self action:@selector(textFieldInputDidChange:) forControlEvents:UIControlEventEditingChanged];
      

      现在,每次用户更改UITextField 中的字符时,您刚刚连接的IBAction 都会被触发。创建一个timer 作为 ivar。现在每次调用 IBAction 时,启动计时器,如果它会达到 2 秒而没有被新呼叫重新启动,则您知道用户尚未在 UITextField 中输入/删除值。

      【讨论】:

      • 这正是我所追求的,也是一个很棒的解决方案。谢谢!
      【解决方案3】:

      我是为 searchBar 做的,但我认为它也适用于 UITextField。代码在 Swift 中。 :)

      func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
          searchTimer?.invalidate()
          searchTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("search"), userInfo: nil, repeats: false)
      }
      
      func search() {
          println("search \(searchBar.text)")
      }
      

      【讨论】:

        【解决方案4】:

        我认为拨打[NSRunLoop cancelPreviousPerformRequestsWithTarget:self] 不是一个好习惯。

        我会这样做:

        [self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        //or select the textfield in the storyboard, go to connections inspector and choose the target for 'Editing Changed'
        
        
        - (IBAction)textFieldDidChange:(id)sender {
            [self performSelector:@selector(editingChanged:) withObject:self.searchTextField.text afterDelay:2.0]; 
        }
        
        - (void)editingChanged:(NSString *)text {
            if ([text isEqualToString:self.searchTextField.text]) {
                //do your thing
            }
        }
        

        这样用户可以输入,它会触发对editingChanged:的调用,然后您可以仔细检查该值是否同时更改,如果没有,则用户停止输入 2 秒。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-03
          • 1970-01-01
          相关资源
          最近更新 更多