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

时间:2015-01-13 07:29:30

标签: ios swift uitextfield

我必须在uitextfield输入自身时允许小于190的值。我们不应允许用户在1之后输入919个数字。

任何人都可以提供一些有关此事的信息。 我尝试了以下代码。但它允许超过190

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

    return floatValue < 190.0
}

3 个答案:

答案 0 :(得分:2)

我将如何设置包含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;
}

答案 1 :(得分:0)

您可以使用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

答案 2 :(得分:0)

// 将文本字段的标签设置为 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
}