需要在iOS中将UITextField的字符输入限制为两个字符
我正在开发一个具有多个UITextFields的应用程序。对于一个UITextField,我已将其委托设置为self,并调用委托方法:需要在iOS中将UITextField的字符输入限制为两个字符,ios,uitextfield,Ios,Uitextfield,我正在开发一个具有多个UITextFields的应用程序。对于一个UITextField,我已将其委托设置为self,并调用委托方法: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - (BOOL)textField:(UITextField *)textField shouldChangeC
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (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;
}
- (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
}
}
完成一项特定的任务。但是,我在同一屏幕上还有其他UITextFields,我想对其执行完全不同的操作,在本例中,将输入的字符数限制为仅两个。不幸的是,我在网上看到的唯一可行的方法是使用上述方法进行限制。如果我已经在使用上述方法为另一个UITextField执行完全不同的操作,那么这是否仍然可行?如果是,如何实现
作为记录,以下是我当前对委托方法的实现:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (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;
}
- (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
}
}
您希望使用相同的方法。见第一部分:
-(BOOL)textField:(UITextField*)textField
该方法允许您标识触发此委托方法的文本字段。然后,您只需要做一些逻辑来为不同的文本字段执行不同的任务
比如:
您可以为每个UITextField设置不同的标记,并获取特定UITextField的textField.tag,以定义委托方法中的行为在类中创建一个
UITextField
属性:
@interface MyObject ()
@property (nonatomic, retain) UITextField *textField1;
@end
self.firstRestrictionTextField.delegate = self;
self.yourSecondTextField.delegate = self;
然后在委托方法中,只需检查文本字段是否与您保存的文本字段相同:
- (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
}
. . .
}
将两个文本字段都用作类中的属性。例如,假设这是控制器的接口
@interface YourViewController : UIViewContoller <UITextFieldDelegate> {
}
/*
* other properties
*/
@property(nonatomic, retain) UITextField *firstRestrictionTextField;
@property(nonatomic, retain) UITextField *yourSecondTextField;
@end
当您实现委托方法时:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (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;
}
- (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
}
}
声明一个文本字段:
@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:shouldChangeCharactersRange:
中,您可以进行不同的测试和行为:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField.tag == 1){
//textfield1
}
//etc
}
非常感谢您的解决方案。您的答案是最完整的:-)由于委托方法将实际的UITextField输入作为参数,因此不需要使用标记(在我看来,标记有点冒险,因为它们本质上是全局变量)。