当我从我的应用程序中选择语言选择列表中的语言时,它会显示我之前选择的语言。如果我从我的模拟器堆栈中清除应用程序或从xcode清除它然后运行项目,之后它就可以了,如果我想再次更改语言,那么我遇到了同样的问题。我的代码如下:
- (IBAction)English:(id)sender {
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];
ChooseItemVC *civc = (ChooseItemVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ChooseItemVC"];
[self.navigationController pushViewController:civc animated:YES];
}
另一种语言选择代码:
- (IBAction)Arabic:(id)sender {
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];
ChooseItemVC *civc = (ChooseItemVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ChooseItemVC"];
[self.navigationController pushViewController:civc animated:YES];
}
答案 0 :(得分:2)
删除所有用户默认值的最佳方法如下: Swift代码:
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
UserDefaults.standard.synchronize()
目标C
NSString *strIdentifier = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] strIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 1 :(得分:0)
从NSUserDefaults
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];
答案 2 :(得分:0)
要删除使用此
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 3 :(得分:0)
从NSUserDefaults删除数据试试这个:
NSString *AppDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:AppDomain];
答案 4 :(得分:0)
使用以下代码删除用户默认值中的所有现有数据,我发布swift代码,将其转换为objective-c。
if let bundleID = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundleID)
}