Objective c 为货币格式自定义UITextField在iOS 4.x中运行良好,但在iOS 5中为空

Objective c 为货币格式自定义UITextField在iOS 4.x中运行良好,但在iOS 5中为空,objective-c,ios,ios5,Objective C,Ios,Ios5,我正在使用以下链接自定义本地货币符号和逗号的UItextField: 在iOS 5中,此实际数字始终为空,而在iOS 4.x中,它工作正常 我的代码用于此目的的主要方法是: -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 1) {

我正在使用以下链接自定义本地货币符号和逗号的UItextField:

在iOS 5中,此实际数字始终为空,而在iOS 4.x中,它工作正常

我的代码用于此目的的主要方法是:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 1)
    {
        if(true)
        {
            NSMutableString* mstring = [[textField text] mutableCopy];
            if([mstring length] == 0)
            {
                //special case...nothing in the field yet, so set a currency symbol first
                [mstring appendString:[[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]];

                //now append the replacement string
                [mstring appendString:string];
            }
            else
            {
                //adding a char or deleting?
                if([string length] > 0)
                {
                    [mstring insertString:string atIndex:range.location];
                }
                else 
                {
                    //delete case - the length of replacement string is zero for a delete
                    [mstring deleteCharactersInRange:range];
                }
            }

            NSString* localeSeparator = [[NSLocale currentLocale]
                                         objectForKey:NSLocaleGroupingSeparator];

            NSNumber *actualNumber = [currencyFormatter numberFromString:[mstring
                                                                    stringByReplacingOccurrencesOfString:localeSeparator
                                                                    withString:@""]];
            NSLog(@"%@",actualNumber);

            [textField setText:[currencyFormatter stringFromNumber:actualNumber]];

            [mstring release];
        }

        //always return no since we are manually changing the text field
        return NO; 
    }
    else
    {
        return YES;
    }
}
这是初始化

NSLocale *paklocal = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_PAK"] autorelease];
currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:0];
[currencyFormatter setLocale:paklocal];

NSMutableCharacterSet *numberSet = [[NSCharacterSet decimalDigitCharacterSet] mutableCopy];
[numberSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
nonNumberSet = [[numberSet invertedSet] retain];
[numberSet release];

我认为您遇到了问题,因为
textField:shouldChangeCharactersRange:replacementString:
[NSLocale currentLocale]
添加了货币符号,这可能与
currencyFormatter
使用的区域设置不同。在我电脑上的模拟器中,它在
mstring
中添加了$(美元)符号,这在逻辑上已经被currencyFormatter拒绝了

构造
paklocal
时,将其与
currencyFormatter
一起存储,并使用它而不是
[NSLocale currentLocale]

如果您在使用
currencyFormatter
时遇到进一步的问题,请使用
NSLog
显示发送到其中的字符串

NSLocale *paklocal = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_PAK"] autorelease];
currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:0];
[currencyFormatter setLocale:paklocal];

NSMutableCharacterSet *numberSet = [[NSCharacterSet decimalDigitCharacterSet] mutableCopy];
[numberSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
nonNumberSet = [[numberSet invertedSet] retain];
[numberSet release];