zoukankan      html  css  js  c++  java
  • 深入理解iOS API系列(一) textField:shouldChangeCharactersInRange:replacementString:

    前言

        深入理解iOS API系列是一个较深解读iOS API的博文集,限于时间和作者精力,内容并不会全部原创,但是涵盖的内容,应该是广大iOS开发者,特别是初中级开发者经常误解或理解不够深刻的部分。

     

    主要是深入理解代理方法textField:shouldChangeCharactersInRange:replacementString:的使用。

    转自http://www.cnblogs.com/Clin/p/3413146.html

     

     

    如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作:

    1、实现UITextFieldDelegate协议;

    2、实现textField:shouldChangeCharactersInRange:replacementString:方法;

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    {

        NSInteger strLength = textField.text.length - range.length + string.length;

        return (strLength <= kMaxLength);

    }

     

    方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    功能:

      把textField中位置为range的字符串替换为string字符串;

      此函数在textField内容被修改时调用;

    返回值:

      YES,表示修改生效;NO,表示不做修改,textField的内容不变。

    参数说明:

      textField:响应UITextFieldDelegate协议的UITextField控件。

      range:    UITextField控件中光标选中的字符串,即被替换的字符串;

              range.length为0时,表示在位置range.location插入string。

      string:    替换字符串;

              string.length为0时,表示删除。

  • 相关阅读:
    简单三层代码生成器原理剖析
    深入浅出三层架构
    SQL Server数据的批量导入
    BULK INSERT如何将大量数据高效地导入SQL Server
    用扩展方法优化多条件查询
    SQL Server 数据库访问通用类(更新)
    SQL中自增量字段Int型 identity(1,1)与Guid作为主键的性能比较
    MD5加盐实现登录
    Pi币
    SQL Server 分割字符串转列
  • 原文地址:https://www.cnblogs.com/ChouDanDan/p/5048326.html
Copyright © 2011-2022 走看看