我正在创建免责声明,该免责声明将在用户首次启动应用时显示。免责声明是一个带有2个选项的alertView。如果用户同意,则会显示firstViewController。如果他没有,他将被重定向到另一个viewController。但如果用户第一次同意,我就不能让免责声明消失。每次应用程序启动时都会显示。任何帮助,将不胜感激。提前谢谢..
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:@"keyDisclaimer"] isEqualToString:@"accepted"]) {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:@"Read Before use" message:@"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"No!" otherButtonTitles:@"Yes Let me In", nil];
[disclaimer show];
}
// Override point for customization after application launch.
return YES;
}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonString = {[alertView buttonTitleAtIndex:buttonIndex]};
if ([buttonString isEqualToString:@"Yes Let me In"]) {
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
[defaultValues setValue:@"accepted"forKey:@"keyDisclaimer"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
else if ([buttonString isEqualToString:@"No!"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"You are not allowed to use this app due to the fact that you did not agree to the terms and Conditions. Please exit this app!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
// [[NSUserDefaults standardUserDefaults] setValue:@"notAccepted" forKey:@"keyDisclaimer"];
}
if ([buttonString isEqualToString:@"OK"]) {
introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = intro;
[_window makeKeyAndVisible];
}
}
答案 0 :(得分:2)
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
[defaultValues setValue:...forKey:...]
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
如果未设置(第一次)
,则会注册默认值此外,您似乎忘记在设置值后[defaults synchronize]
提交更改。如果是这样,您根本不需要registerDefaults
方法。
像这样:
if ([buttonString isEqualToString:@"Yes Let me In"]) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"accepted"forKey:@"keyDisclaimer"];
[defaults synchronize];
}
答案 1 :(得分:0)
你想使用stringForKey或objectForKey而不是valueForKey。
if([[defaults stringForKey:@"keyDisclaimer"] isEqualToString:@"accepted"]) {
过去我曾经遇到过使用valueForKey的糟糕经历并不总是以我想要的方式工作。这可能是导致你出现问题的原因。