在模拟器中更改“设置”中的“区域格式”,但应用中的区域设置不会更改

时间:2014-12-03 09:40:50

标签: ios xcode internationalization simulator

我使用NSDateFormatter的实例来帮助在我的应用程序中显示日期字符串,但在我更改了模拟器中的设置中的区域格式后,来自"美国"到"英国",设置中的示例显示日期为" 5,2014年1月和#34;,这证明更改成功。但是当我在模拟器中打开我的应用程序时,日期仍然是" 2014年1月5日"。 我使用以下代码检查我的应用程序中的区域设置:

NSLocale *locale = [NSLocale currentLocale];
NSString *currentLocale = [locale objectForKey:NSLocaleIdentifier];
NSLog(@"Current locale is %@", currentLocale);

并记录:当前区域设置为en_US。 似乎我的应用程序在模拟器设置中没有获得当前的区域设置。它应该是我在模拟器设置中更改区域设置,我的应用程序可以反映更改。有什么不对的吗? 日期格式代码是

static NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];

我使用的是iOS模拟器版本8.1,Xcode版本6.1.1,iOS 8.1,SDK 8.1

将dateFormatter代码放在一个视图控制器的viewWillAppear方法中,在区域格式更改后,创建一个新的视图控制器仍然呈现旧的语言环境。相同的结果甚至杀了应用程序。我在表视图中注册区域设置更改通知并刷新它。它可以反映出语言环境的变化。无法理解其中的差异。

2 个答案:

答案 0 :(得分:1)

我有同样的问题。它看起来像模拟器中的一个错误:https://devforums.apple.com/message/1063365

答案 1 :(得分:0)

您必须设置NSDateFormatter

的区域设置
NSLocale *locale = [NSLocale currentLocale];
NSString *currentLocale = [locale objectForKey:NSLocaleIdentifier];
NSLog(@"Current locale is %@", currentLocale);


static NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:locale]; // <- Here

dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];