2

我正在开发一个具有多个 UITextFields 的应用程序。对于一个 UITextField,我已将其委托设置为 self 并调用委托方法:

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

完成一项特定的任务。但是,我在同一个屏幕上有其他 UITextField,我想做一些完全不同的事情,在这种情况下,将输入的字符数限制为两个。不幸的是,我在网上看到的唯一可能的方法是使用上述方法进行限制。如果我已经在使用上述方法为另一个 UITextField 做一些完全不同的事情,这仍然可能吗?如果是这样,怎么做?

作为记录,这是我当前的委托方法实现:

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

    if([[string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]]
        isEqualToString:@""])
        return YES;

    NSString *previousValue = [[[textField.text stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@""];
    string = [string stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
    NSString *modifiedValue = [NSString stringWithFormat:@"%@%@", previousValue, string];

    if ([modifiedValue length] == 1) {

        modifiedValue = [NSString stringWithFormat:@"0.0%@", string];

    }

    else if ([modifiedValue length] == 2) {

        modifiedValue = [NSString stringWithFormat:@"0.%@%@", previousValue, string];

    }

    else if ([modifiedValue length] > 2) {

        modifiedValue = [NSString stringWithFormat:@"%@.%@",[modifiedValue substringToIndex: modifiedValue.length-2],[modifiedValue substringFromIndex:modifiedValue.length-2]];

    }


    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:modifiedValue];
    modifiedValue = [formatter stringFromNumber:decimal];
    textField.text = modifiedValue;

    return NO;

}
4

5 回答 5

2

将两个文本字段用作类中的属性。例如,这是您的控制器的接口。

@interface YourViewController : UIViewContoller <UITextFieldDelegate> {
}

/*
* other properties
*/
@property(nonatomic, retain) UITextField *firstRestrictionTextField;
@property(nonatomic, retain) UITextField *yourSecondTextField;

@end

在您的实现中,两个文本字段都应设置为您的类的委托:

self.firstRestrictionTextField.delegate = self;
self.yourSecondTextField.delegate = self;

当您实现委托方法时:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.firstRestrictionTextField) {
// Do stuff you need in first textfield
}
if (textField == self.yourSecondTextField) {
// Do stuff for your second textfield
}

}
于 2013-03-29T02:38:33.050 回答
1

在你的类中创建一个UITextField属性:

@interface MyObject ()
@property (nonatomic, retain) UITextField *textField1;
@end

然后在委托方法中,只需检查文本字段是否与您保存的相同:

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

    if (textField == [self textField1]) {
        // do stuff here
    } else {
        // do stuff here for other text fields
    }

    . . .
}
于 2013-03-29T02:36:51.900 回答
0

您想使用相同的方法。见第一部分:

- (BOOL)textField:(UITextField *)textField

该方法允许您识别哪个 textField 正在触发此委托方法。然后你只需要做一些逻辑来为不同的 textFields 执行不同的任务。

像:

if (textField1){action 1}
else if (textField2){action 2}
else {default action}
于 2013-03-29T02:35:13.600 回答
0

声明一个文本字段:

    @property(nonatomic, strong)UITextField *textField1;
    @property(nonatomic, strong)UITextField *textField2;
    //etc

给它一个标签:

self.textField1.tag = 1;//or whatever
self.textField2.tag = 2;//or whatever
//etc

然后在 中textField:shouldChangeCharactersInRange:,您可以进行不同的测试和行为:

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

if(textField.tag == 1){
//textfield1
}
//etc
}
于 2013-03-29T02:42:06.307 回答
-1

您可以为每个 UITextField设置不同的标签,并获取特定 UITextField 的textField.tag以定义委托方法中的行为

于 2013-03-29T02:35:49.123 回答