2013-03-29 27 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 
} 
相關問題