UITextField Should accept numbers only in iphone

前端 未结 3 1959
我在风中等你
我在风中等你 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];
      }
      
    0 讨论(0)
  • 2021-01-27 14:40

    If you have textFiled available while coding, set keyboardType property of UITextField.

     textField.keyboardType = UIKeyboardTypeNumberPad;
    

    Or you can set the same with in xib file.

    0 讨论(0)
  • 2021-01-27 14:53

    In your xib file select the textFiled and in the property list there is a field Keyboard select Number Pad

    0 讨论(0)
提交回复
热议问题