UIAlerview 的 NSUserDefaults

NSUserDefaults for UIAlerview

这是我的代码:

  #import "RootViewController.h"

@implementation RootViewController
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.view.backgroundColor = [UIColor blackColor];
    UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://deathsrepo.pw"]]];
UIAlertView *webAlert = [[UIAlertView alloc]
                         initWithTitle:@"Technologx" message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:@"OK", nil];
[webAlert show];
[webAlert release];
}
@end

如何让我的 AlertView window 只显示一次。我希望用户能够按 'OK' 并且当他们再次打开应用程序时它不会弹出但是如果他们只按 "done" 它会吗?

#import "RootViewController.h"
NSInteger YourInt;
@interface RootViewController () <UIAlertViewDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    YourInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"Saved"];
    if (YourInt == 0) {
        UIAlertView *Webalert = [[UIAlertView alloc] initWithTitle:@"Technologx"  message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them." delegate:self cancelButtonTitle:@"Done"  otherButtonTitles:@"Ok", nil];
    [Webalert show];
    }

}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"Done");
    } else if (buttonIndex == 1) {
        NSLog(@"Ok");
        YourInt = 1;
        [[NSUserDefaults standardUserDefaults] setInteger:YourInt forKey:@"Saved"];
    }
}

希望对您有所帮助!

首先,警报视图已弃用。使用如下所示的警报控制器。其次,切换放置代码的位置。我建议在 viewDidLoad: 中加载 Web 视图,但为了简单起见,让我们继续这样做。

- (void)viewDidAppear:(BOOL)animated {

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.view.backgroundColor = [UIColor blackColor];

    UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://deathsrepo.pw"]]];


if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstKey"]!=YES) {
UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"Technologx"
                                    message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them."
                                    preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:YES completion:nil];
}



UIAlertAction* ok = [UIAlertAction
                        actionWithTitle:@"OK"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {

[[NSUserDefaults standardUserDefaults] setBOOL:YES forKey:@"firstKey"];

                        }];


[webAlert release];
}




@end

让我们谈谈我们在做什么。我们检查用户默认密钥是否为 nil,如果是,则向他们显示 sheet。如果他们曾经在 sheet 上单击过“确定”,这意味着他们以前见过它,我们会设置一个处理程序,将任何东西添加到密钥中。因此,密钥不会再为零,所以你的 sheet 一旦他们看到它就再也不会出现了。