如何不允许用户在 uitextfield 中输入超过 190 的值?

How to not allow the user to enter a value more than 190 in uitextfield?

我必须在 uitextfield 中允许小于 190 的值,而输入 itself.we 不应允许用户在 19 之后输入 19 数字.

任何人都可以向我提供一些有关此的信息。 我尝试了下面的 code.But 它允许超过 190.

if countElements(textField.text!) + countElements(string) - range.length < 4
{
    var floatValue : Float = NSString(string: toString(textField.text)).floatValue

    return floatValue < 190.0
}

我的做法是将包含 uitextfield 的 uiviewcontroller 设置为文本字段的委托。然后添加:

//If number of characters needs to be less than 190
- (BOOL)textField:(UITextField *)textField 
        shouldChangeCharactersInRange:(NSRange)range 
        replacementString:(NSString *)string 
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if([newString length]>190){
        return NO;
    }
    return YES;
}

//If value needs to be less than 190
- (BOOL)textField:(UITextField *)textField 
    shouldChangeCharactersInRange:(NSRange)range 
    replacementString:(NSString *)string   
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if([newString intValue]>190){
        return NO;
    }
    return YES;
}

您可以使用 UITextFieldTextDidChangeNotification,然后在通知的选择器中检查值。这是我在 Xcode:

中验证的实施示例
#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.textField.delegate = self;
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textFieldDidChange
{
    if ([self.textField.text length] > 3) {
        NSString* subString = [self.textField.text substringWithRange:NSMakeRange(0, 3)];
        self.textField.text = subString;
    }
    if ( [self.textField.text intValue] > 190)
    {
        NSString* subString = [self.textField.text substringWithRange:NSMakeRange(0, 2)];
        self.textField.text = subString;
    }
}

@end

// 将文本字段的标签设置为 1
// 在“yourtextfield”下方的代码中,将其替换为您的文本字段参考名称

 public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    if textField.tag == 1  {
        let newLength = (textField.text?.utf16.count)! + string.utf16.count - range.length
        if newLength >= 3 {
            
            let newstr = (textField.text ?? "") + string
            if let value = Int(newstr), value > 109 {
                self.yourtextfield.text = ""
                self.yourtextfield.placeholder = "Enter lesser than or equal to 109"
                return false
            }else
            {
                return true
            }
        } else {
            return true
        }
    }
    return true
}