IOS开发问题解决之限制UITextField输入特定位数小数时,Done按钮失效

来源:互联网 发布:短作业优先算法例题 编辑:程序博客网 时间:2024/05/01 19:29
 

IOS开发问题解决之限制UITextField输入特定位数小数时,Done按钮失效

分类: IOS开发问题解决 1045人阅读 评论(0) 收藏 举报
iosstring任务测试

目录(?)[+]

问题描述

      要求是限制UITextField只能输入一位小数。我的方法是重写delegate的textField:shouldChangeCharactersInRange:replacementString:函数。自己写的代码如下:

[cpp] view plaincopy
  1. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
  2.       
  3.     isHasRadixPoint = YES;  
  4.     NSString *existText = textField.text;  
  5.     if ([existText rangeOfString:@"."].location == NSNotFound) {  
  6.         isHasRadixPoint = NO;  
  7.     }  
  8.     if (string.length > 0) {  
  9.         unichar newChar = [string characterAtIndex:0];  
  10.         if ((newChar >= '0' && newChar <= '9') || newChar == '.' ) {  
  11.             if (newChar == '.') {  
  12.                 if (isHasRadixPoint)   
  13.                     return NO;  
  14.                 else   
  15.                     return YES;                  
  16.             }else {  
  17.                 if (isHasRadixPoint) {  
  18.                     NSRange ran = [existText rangeOfString:@"."];  
  19.                     int radixPointCount = range.location - ran.location;  
  20.                     if (radixPointCount <= RadixPointNum) return YES;  
  21.                     else return NO;  
  22.                 } else   
  23.                     return YES;  
  24.             }  
  25.               
  26.         }else {  
  27.             return NO;  
  28.         }  
  29.           
  30.     }else {  
  31.         return YES;  
  32.     }  
  33. }  

     写完测试,这时问题来了。键盘上的"Done"按钮失效了。上面代码中的RandixPointNum是在文件最上边部分定义的宏,代表小数点位数。

解决方法

     “Done”按钮其实就是字符“\n”,由于上面的代码将其过滤了,导致了其事件失效。修改后代码如下,注意有注释的那行:

[cpp] view plaincopy
  1. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
  2.       
  3.     isHasRadixPoint = YES;  
  4.     NSString *existText = textField.text;  
  5.     if ([existText rangeOfString:@"."].location == NSNotFound) {  
  6.         isHasRadixPoint = NO;  
  7.     }  
  8.     if (string.length > 0) {  
  9.         unichar newChar = [string characterAtIndex:0];  
  10.         if ((newChar >= '0' && newChar <= '9') || newChar == '.' ) {  
  11.             if (newChar == '.') {  
  12.                 if (isHasRadixPoint)   
  13.                     return NO;  
  14.                 else   
  15.                     return YES;                  
  16.             }else {  
  17.                 if (isHasRadixPoint) {  
  18.                     NSRange ran = [existText rangeOfString:@"."];  
  19.                     int radixPointCount = range.location - ran.location;  
  20.                     if (radixPointCount <= RadixPointNum) return YES;  
  21.                     else return NO;  
  22.                 } else   
  23.                     return YES;  
  24.             }  
  25.               
  26.         }else {  
  27.             if ( newChar == '\n'return YES;       // 这句非常重要:不然将导致“Done”按钮失效  
  28.             return NO;  
  29.         }  
  30.           
  31.     }else {  
  32.         return YES;  
  33.     }  
  34. }  

0 0
原创粉丝点击