0

I am firing the following function on "Editing Changed" on a UITextField. The purpose is to format the number as the user types in. It is working fine on iOS6+ but the app freezes on iOS5 just as the user types the first character in that field. Can somebody help to find out what might be the issue with this 3 line function. It freezes on the third line where NSNumberFormatter is being used.

-(IBAction)formatNumber:(UITextField*)sender
{
if(sender.text!=NULL)
{
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
NSString *currentVal=[[sender.text componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];

sender.text=[NSNumberFormatter localizedStringFromNumber:[NSNumber numberWithInt:[currentVal intValue]]
                                 numberStyle:NSNumberFormatterDecimalStyle];
}
}
4

1 回答 1

1

问题是每次发送方中的某些内容发生更改时都会调用事件 UIControlEventEditingChanged。

通过设置新文本:

sender.text=[NSNumberFormatter localizedStringFromNumber:[NSNumber numberWithInt:[currentVal intValue]]
                             numberStyle:NSNumberFormatterDecimalStyle];

您实际上是一次又一次地发送事件 UIControlEventEditingChanged ,导致无限递归。

于 2013-06-25T07:48:13.797 回答