【问题标题】:How to refresh the NSLocale in iPhone?如何在 iPhone 中刷新 NSLocale?
【发布时间】:2012-07-21 19:02:13
【问题描述】:

我目前在 iPhone 应用程序中工作,使用 NSLocale 使当前货币符号正常工作,但是当我按下主页按钮时,应用程序进入后台进程并更改货币(设置>>国际>>区域格式>>喜欢美国,印度等...)以这种方式,然后从后台打开该应用程序,货币符号未更改,但我导航到另一个屏幕,然后进入前一个屏幕,仅更改货币,但我希望从后台打开应用程序, 然后货币符号自动更改。如何解决这个问题?我需要你的帮助

谢谢

我在这里尝试了代码:

NSLocale *theLocale = [NSLocale currentLocale];
Getdollarsymbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSString *Code = [theLocale objectForKey:NSLocaleCurrencyCode];

【问题讨论】:

  • 这可能是你可以放手的东西。真实用户不止一次更改其语言环境的可能性很小。也就是说,您是在 viewDidLoad 还是 viewWillAppear 中运行此代码?我似乎记得背景可能存在差异。不过,这可能是错误的。

标签: iphone ios refresh currency nslocale


【解决方案1】:

下面的呢:

[NSLocale autoupdatingCurrentLocale]

Apple docs 提及

对象始终反映当前用户区域设置的当前状态。

【讨论】:

  • 再次出现同样的问题。
  • 我试过这种方式 [NSLocale autoupdatingCurrentLocale] NSLocale *theLocale = [NSLocale currentLocale]; Getdollarsymbol = [theLocale objectForKey:NSLocaleCurrencySymbol]; NSString *Code = [theLocale objectForKey:NSLocaleCurrencyCode];
【解决方案2】:

请尝试此代码。让我知道这是否有效,

-viewDidLoad:添加行:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCurrencySymbol) name:NSCurrentLocaleDidChangeNotification object:nil];

然后:

-(void) refreshCurrencySymbol
{ 
    NSLocale *theLocale = [NSLocale currentLocale];
    NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
    NSLog(@"Symbol : %@",symbol);
    NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];
    NSLog(@"Code : %@",code);
}

谢谢。

【讨论】:

  • 确保将每个 addObserver 与等效的 removeObserver 匹配
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多