2013-03-29 29 views
2

我正在研究具有多个UITextField的应用程序。对于一个的UITextField,我已经设置其委托的自我和我打电话的委托方法:需要将UITextField的字符输入限制为iOS中的两个字符

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

做特定任务。但是,我在其他UITextFields的屏幕上我想做一些完全不同的事情,在这种情况下,将输入的字符数限制为两个。不幸的是,我在网上看到的唯一可行的方法是使用上述方法进行限制。如果我已经使用上述方法为另一个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; 

} 

回答

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 
} 

} 
+0

非常感谢您的解决方案。你的答案是最完整的:-) – syedfa

0

你想用这个方法相同。见第一部分:

- (BOOL)textField:(UITextField *)textField

的方法可以让你识别文本字段是触发该委托方法。然后,您只需执行一些逻辑即可为不同的textField执行不同的任务。

像:

if (textField1){action 1} 
else if (textField2){action 2} 
else {default action} 
-1

可以设置不同的标签每个的UITextField,并得到textField.tag为特定的UITextField来定义委托方法的行为

+0

由于委托方法以实际的UITextField输入作为一个参数,也没有必要使用标签(这是一个无论如何,海事组织,因为它们本质上是全球变数)。 – DanM

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 
    } 

    . . . 
} 
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 
} 
相关问题