ios 仅允许 UITextField 输入的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12944789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:49:22  来源:igfitidea点击:

Allow only Numbers for UITextField input

iosipadcocoa-touchuitextfielduikeyboard

提问by Demasterpl

The iPad does not have a "Numpad" keyboard like the iPhone/iPod does.

iPad 没有像 iPhone/iPod 那样的“Numpad”键盘。

I'm looking to find how I can restrict the user's keyboard to only accept values 0 through 9.

我正在寻找如何将用户的键盘限制为仅接受 0 到 9 的值。

I would imagine using UITextField's "shouldChangeCharactersInRange" but I don't know the best way to implement it.

我想使用 UITextField 的“shouldChangeCharactersInRange”,但我不知道实现它的最佳方法。

回答by Beltalowda

This is how you might handle the problem on a SSN verification field, you can modify the max length and remove the ifstatement checking for keyboard type if you need to.

这就是您处理 SSN 验证字段问题的方式,您可以修改最大长度并根据需要删除if检查键盘类型的语句。

There is also logic to suppress the max length alerts when the user is typing as opposed to pasting data.

当用户键入而不是粘贴数据时,还有逻辑可以抑制最大长度警报。

Within the context of this code, presentAlert()/presentAlert:is just some basic function that presents a UIAlertController(or a legacy UIAlertView) using the message string passed.

在此代码的上下文中,presentAlert()/presentAlert:只是一些使用传递的消息字符串呈现UIAlertController(或遗留UIAlertView)的基本函数。

Swift 5

斯威夫特 5

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
// object that will contain this code, because otherwise it would never be called.
//
// There are also some better stylistic approaches in Swift to avoid all the 
// nested statements, but I wanted to keep the styles similar to allow others 
// to contrast and compare between the two languages a little easier.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Handle backspace/delete
    guard !string.isEmpty else {

        // Backspace detected, allow text change, no need to process the text any further
        return true
    }

    // Input Validation
    // Prevent invalid character input, if keyboard is numberpad
    if textField.keyboardType == .numberPad {

        // Check for invalid input characters
        if CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) {

            // Present alert so the user knows what went wrong
            presentAlert("This field accepts only numeric entries.")

            // Invalid characters detected, disallow text change
            return false
        }
    }

    // Length Processing
    // Need to convert the NSRange to a Swift-appropriate type
    if let text = textField.text, let range = Range(range, in: text) {

        let proposedText = text.replacingCharacters(in: range, with: string)

        // Check proposed text length does not exceed max character count
        guard proposedText.count <= maxCharacters else {

            // Present alert if pasting text
            // easy: pasted data has a length greater than 1; who copy/pastes one character?
            if string.count > 1 {

                // Pasting text, present alert so the user knows what went wrong
                presentAlert("Paste failed: Maximum character count exceeded.")
            }

            // Character count exceeded, disallow text change
            return false
        }

        // Only enable the OK/submit button if they have entered all numbers for the last four
        // of their SSN (prevents early submissions/trips to authentication server, etc)
        answerButton.isEnabled = (proposedText.count == 4)
    }

    // Allow text change
    return true
}

Objective-C

目标-C

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
// object that will contain this code, because otherwise it would never be called.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Handle backspace/delete
    if (!string.length)
    {
        // Backspace detected, allow text change, no need to process the text any further
        return YES;
    }

    // Input Validation
    // Prevent invalid character input, if keyboard is numberpad
    if (textField.keyboardType == UIKeyboardTypeNumberPad)
    {
        if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound)
        {
            [self presentAlert: @"This field accepts only numeric entries."];
            return NO;
        }
    }

    // Length Validation
    NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    // Check proposed text length does not exceed max character count
    if (proposedText.length > maxCharacters)
    {
        // Present alert if pasting text
        // easy: pasted data has a length greater than 1; who copy/pastes one character?
        if (string.length > 1)
        {
            // Pasting text, present alert so the user knows what went wrong
            [self presentAlert: @"Paste failed: Maximum character count exceeded."];
        }

        // Character count exceeded, disallow text change
        return NO;
    }

    // Only enable the OK/submit button if they have entered all numbers for the last four
    // of their SSN (prevents early submissions/trips to authentication server, etc)
    self.answerButton.enabled = (proposedText.length == maxCharacters);

    // Allow text change
    return YES;
}

回答by Aje

You can use this code to allow only number in textField.

您可以使用此代码在 textField 中只允许数字。

Before that set delegate for textField

在为 textField 设置委托之前

      textFieldName.delegate=self;

or

或者

      [textFieldName setDelegate:self];

Than use this code to allow only digit to textField

比使用此代码只允许数字到 textField

      - (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {
//return yes or no after comparing the characters

      // allow backspace
      if (!string.length)
      {
           return YES;
      }

      ////for Decimal value start//////This code use use for allowing single decimal value
      //    if ([theTextField.text rangeOfString:@"."].location == NSNotFound)
      //    {
      //        if ([string isEqualToString:@"."]) {
      //            return YES;
      //        }
      //    }
      //    else
      //    {
      //        if ([[theTextField.text substringFromIndex:[theTextField.text rangeOfString:@"."].location] length]>2)   // this allow 2 digit after decimal 
      //        {
      //            return NO;
      //        }
      //    }
      ////for Decimal value End//////This code use use for allowing single decimal value

      // allow digit 0 to 9
      if ([string intValue])
      {
            return YES;
      }

      return NO;
    }

回答by SPatel

Try this to avoid textfield clearing issue

试试这个以避免文本字段清除问题

Swift 3.0

斯威夫特 3.0

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {
        return false
    }
    return true
}

Swift 4.0

斯威夫特 4.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
        return false
    }
    return true
}

回答by whyceewhite

Very specific steps for Swift code

Swift 代码的非常具体的步骤

You can provide logic that restricts the text field's input in the func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Boolmethod by implementing the UITextFieldDelegateprotocol.

您可以func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool通过实现UITextFieldDelegate协议来提供限制方法中文本字段输入的逻辑。

For the sake of clarity, these steps assume that your storyboard contains a View Controllerwith a text fieldobject that should only accept digits.

为清楚起见,这些步骤假设您的故事板包含一个视图控制器,其中包含一个只接受数字的文本字段对象。

  1. Create a custom class for the view controller that extends UIViewController. Make surethat the scene in your storyboard refers to the custom class by setting the custom class value in Xcode's Identity Inspector.

    import UIKit
    class YourCustomController: UIViewController {
        override func viewDidLoad() {        
            super.viewDidLoad()
        }
    }
    
  2. Create an outlet from your scene's text field to your custom View Controller.

    class YourCustomController: UIViewController {
        @IBOutlet weak var numberField: UITextField!
        ...
    }
    
  3. Apply the UITextFieldDelegateprotocol in your custom view controller.

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
    }
    
  4. In your custom view controller's viewDidLoadmethod, assign your text field's delegate to your custom view controller class.

    override func viewDidLoad() {        
        super.viewDidLoad()
        numberField.delegate = self
    }
    
  5. Add the UITextFieldDelegate's func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Boolmethod.

    As a result of making your custom view controller the numberField's delegate in the previous step, this method will be called each time a user enters a character into the text field. If your method returns truethen the character will remain in the text field. If your method returns falsethen the character will notremain in the text field.

    The stringparameter is the character being entered by the user. If the stringcharacter can be converted to an Intthen it is between 0 and 9; otherwise, it is some non-number character.

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
            return Int(string) != nil
        }
    }
    
  1. 为扩展的视图控制器创建一个自定义类UIViewController。通过在 Xcode 的 Identity Inspector 中设置自定义类值,确保故事板中的场景引用自定义类。

    import UIKit
    class YourCustomController: UIViewController {
        override func viewDidLoad() {        
            super.viewDidLoad()
        }
    }
    
  2. 创建从场景文本字段到自定义视图控制器的出口。

    class YourCustomController: UIViewController {
        @IBOutlet weak var numberField: UITextField!
        ...
    }
    
  3. UITextFieldDelegate在您的自定义视图控制器中应用协议。

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
    }
    
  4. 在自定义视图控制器的viewDidLoad方法中,将文本字段的委托分配给自定义视图控制器类。

    override func viewDidLoad() {        
        super.viewDidLoad()
        numberField.delegate = self
    }
    
  5. 添加UITextFieldDelegatefunc textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool方法。

    由于numberField在上一步中使您的自定义视图控制器成为的委托,因此每次用户在文本字段中输入字符时都会调用此方法。如果您的方法返回,true则该字符将保留在文本字段中。如果您的方法返回,false则该字符将不会保留在文本字段中。

    string参数是由用户输入的字符。如果string字符可以转换为 anInt那么它在 0 到 9 之间;否则,它是一些非数字字符。

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
            return Int(string) != nil
        }
    }
    

(See below for the full view controller code.)

(有关完整的视图控制器代码,请参见下文。)



Example View Controller with digits only text field

带有仅数字文本字段的示例视图控制器

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {        
        return Int(string) != nil
    }    
}


Example View Controller with a Decimal text field

带有十进制文本字段的示例视图控制器

If you want to support a decimal number then take advantage of NSNumberFormatter. See the code comments for the differences.

如果您想支持十进制数,请利用 NSNumberFormatter. 有关差异,请参阅代码注释。

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    private var formatter: NSNumberFormatter!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self

        // Initialize the formatter; minimum value is set to zero; style is Decimal. 
        formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        formatter.minimum = 0
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        // Combine the current text field value and the new string
        // character. If it conforms to the formatter's settings then
        // it is valid. If it doesn't then nil is returned and the
        // string character should not be allowed in the text field.         
        return formatter.numberFromString("\(textField.text)\(string)") != nil
    }    
}

回答by Adobels

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

    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterNoStyle];

    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];

    if (number)
        return YES;
    else
       return NO;
}

回答by iDev

I applied this and it works!!

我应用了这个,它有效!!

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Check for non-numeric characters
NSUInteger lengthOfString = string.length;
for (NSInteger index = 0; index < lengthOfString; index++) {
    unichar character = [string characterAtIndex:index];
    if (character < 48) return NO; // 48 unichar for 0
    if (character > 57) return NO; // 57 unichar for 9
}
// Check total length for restrict user
NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
if (proposedNewLength > 6)
    return YES;
return YES;                                                                                                                                     
}

回答by johnny

NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
    if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
        return NO;
    }

回答by Ankit Kumar Gupta

Works fine for me :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound) && !(range.length==1 && string.length==0)) {
            return NO;
        }
        return YES;
    }

回答by mishimay

In Swift:

在斯威夫特:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return string.isEmpty || Int(string) != nil
    }

回答by Younes Idrissi

swift 5

迅捷 5

    //MARK:- UITextFieldDelegate

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let allowedCharacters = "1234567890"
    let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
    let typedCharcterSet = CharacterSet(charactersIn: string)
    return allowedCharcterSet.isSuperset(of: typedCharcterSet)
}

You can now just tap 1234567890 only

您现在只需点击 1234567890