UITextField Should accept numbers only in iphone

前端 未结 3 1961
我在风中等你
我在风中等你 2021-01-27 14:05

UITextField have to accept the numbers only by using UITableView.

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 14:40

    You have to perform two steps:

    1. Provide keyboard type as UIKeyboardTypeNumberPad, as mentioned by others.
    2. In

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

      method check that entered string in numeric or not.You can use following method to check numeric value:

      -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      {
          if([self isNumeric:string])
              return TRUE;
          else
              return FALSE;
      }
      
      -(BOOL)isNumeric:(NSString*)inputString
      {
          NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
          NSString *filtered;
          filtered = [[inputString componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
          return [inputString isEqualToString:filtered];
      }
      

提交回复
热议问题