【问题标题】:shouldChangeCharactersInRange affecting all UITextField'sshouldChangeCharactersInRange 影响所有 UITextField 的
【发布时间】:2013-04-25 18:19:39
【问题描述】:

我的应用中有两个UITextField,一个是价格,另一个是产品标签。

我在 .m 中定义了 UITextField 和 @property@synthesize

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

我正在使用这种方法来限制价格字段的输入,但它似乎会影响这两个字段。如何将其限制为一个字段?

【问题讨论】:

    标签: uitextfield


    【解决方案1】:

    当任何 UITextField 将此实例设置为 Interface Builder 中的委托或使用代码时,将调用该方法。 您可以使用以下内容检查哪个字段调用它:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if(textField == yourSynthesizedPropertyForPriceField) {
            //DO SOMETHING
        }
        return YES;
    }
    

    【讨论】: