zoukankan      html  css  js  c++  java
  • 监测UITextField的变化

    监测UITextField的变化可以为UIControlEventEditingChanged事件添加target。

    我们有时候会需要用到这个需求:输入框输入文本超过xx长度,不再允许输入其他内容!

    UITextField 代理方法本身是无法满足这个需求的。(当然你可以给UITextView添加placeholder实现相同的需求,此处不做介绍。)

    示例代码如下:

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.f, 100.f, CGRectGetWidth(self.view.frame) - 40.f, 30.f)];
        textField.backgroundColor = [UIColor whiteColor];
        textField.placeholder = @"placeholder_King";
        [self.view addSubview:textField];
        // 添加 UIControlEventEditingChanged  target事件,即可实时监测textField 内容,并做一些操作
        [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    - (void)textFieldDidChange:(UITextField *)textField {
        if (textField == self.textField) {
            if (textField.text.length > 40) {
                textField.text = [textField.text substringToIndex:40];
            }
        }
    }

    参考资料:

    传送门一(Swift)

    传送门二(OC)

  • 相关阅读:
    hdu 2203 亲和串
    hdu 3999 The order of a Tree
    poj 2299 Ultra-QuickSort
    hdu 1908 Double Queue
    hdu 1556 Color the ball
    hdu 4288 Coder
    hdu 5265 pog loves szh II
    hdu 5058 So easy
    T103763 【模板】矩阵乘法
    T103492 【模板】点双连通分量
  • 原文地址:https://www.cnblogs.com/xiu619544553/p/5613519.html
Copyright © 2011-2022 走看看