我已经设置了我的应用程序以支持英语的基础国际化,并为西班牙语添加了本地化。我正在尝试提供一个选项,强制应用程序使用用户通过分段控件选择区域设置。
我想要做的是使用新选择的区域设置“刷新”或“重绘”屏幕。
以下是我对分段控件的操作:
- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
NSUserDefaults *prefs;
prefs = [NSUserDefaults standardUserDefaults];
if (selectedSegment == 0) {
[prefs setObject:@"en" forKey:@"language"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"English");
}
else
{
[prefs setObject:@"es" forKey:@"language"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"es", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Spanish");
}
[self.view setNeedsDisplay];
}
我希望屏幕会刷新并显示所选的语言视图。如果我关闭应用程序并重新启动,它会显示新视图。
这是我的观点WillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// get the saved language preference
NSUserDefaults *prefs;
prefs = [NSUserDefaults standardUserDefaults];
NSString *stringVal = [prefs stringForKey:@"language"];
// Make sure we have a valid value
if (stringVal != nil) {
// Set the language preference
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:stringVal, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
// Turn on the appropriate segment
if ([stringVal isEqual: @"en"]) {
NSLog(@"English");
languageSwitch.selectedSegmentIndex = 0;
} else {
NSLog(@"Spanish");
languageSwitch.selectedSegmentIndex = 1;
}
} else {
// default to English
languageSwitch.selectedSegmentIndex = 0;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
}
}
我原以为以下会强制屏幕刷新,但似乎没有效果:
[self.view setNeedsDisplay];
任何想法或建议将不胜感激。谢谢你提前。