iPhone SDK 3.0问题 [英] iPhone sdk 3.0 issue

查看:43
本文介绍了iPhone SDK 3.0问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将maxlength设置为textField,当我在textField中输入最大字符数时,退格键不起作用.因此,我无法更改textFields的内容.当我在iPhone sdk 3.0上测试我的应用程序但在iPhone sdk 2.2上正常运行时,会发生这种情况.

I set maxlength to my textField, when I entered maximum characters in the textField the backspace is not working. Due to that I am unable to change the textFields content. This happens when I test my application on iPhone sdk 3.0 but it works properly in iPhone sdk 2.2.

这是我的代码,

- (BOOL)textField:(UITextField *)txtField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (txtField.text.length >= 15 && range.length == 0)
    {
        return NO; 
    }
    else
    {
        return YES;
    }
}

为什么在iPhone SDK 3.0中会发生这种情况?

why it happens in iPhone sdk 3.0?

推荐答案

在我的头顶上, range.length == 0 对于退格字符将返回true,因此当用户在全文字段上点击退格键时,显式返回 NO .我建议将第一个条件更改为:

Off the top of my head, the range.length == 0 would return true for the backspace character, so you're explicitly returning NO when the user taps backspace on a full text field. I'd suggest changing the first condition to read:

if ( txtField.text.length + range.length > 15 )
    return ( NO );

…以这种方式,您正在测试修改过的字符串是否会太大而无法容纳(而不是仅仅检查现有值和附加值的大小),并且不会跌倒与OS 3.0中的新粘贴代码不符,后者可能一次在文本字段中添加多个字符.

…this way you're testing whether the modified string would be too large to fit (rather than just checking the individual sizes of the existing value and the addition), and you'll not fall afoul of the new paste code in OS 3.0 which could add more than a single character to the text field at a time.

这篇关于iPhone SDK 3.0问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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