我正在创建一个免责声明,它将在用户首次启动该应用程序时显示。免责声明是带有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];
   }
 }

最佳答案

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];
}

关于ios - NSUserDefaults和alertView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12743334/

10-10 08:18