如何不允许用户在 uitextfield 中输入大于 190 的值? [英] How to not allow the user to enter a value more than 190 in uitextfield?

查看:28
本文介绍了如何不允许用户在 uitextfield 中输入大于 190 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在输入自身时允许 uitextfield 中小于 190 的值.我们不应该允许用户在输入 19 数字之后19.

I have to allow values less than 190 in uitextfield while entering itself.we should not allow user to enter 1 to 9 digits after 19.

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

Can anyone please provide me some info regarding this. I tried the below code.But it's allowing more than 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 设置为文本字段的委托.然后添加:

How i would do it is set the the uiviewcontroller containing the uitextfield as the delegate of the text field. Then add this:

//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;
}

这篇关于如何不允许用户在 uitextfield 中输入大于 190 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆