如何使UIAlertView在iPhone应用程序首次启动时只显示一次?

如何使UIAlertView在iPhone应用程序首次启动时只显示一次?,iphone,cocoa-touch,xcode,uialertview,Iphone,Cocoa Touch,Xcode,Uialertview,我正在使用UIAlertView在应用程序启动后弹出一个窗口。它工作正常,但我只希望弹出窗口出现在应用程序的第一次启动。目前,我在applicationdFinishLaunching方法的AppDelegate类中获得了UIAlertView。这是我的密码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { slee

我正在使用
UIAlertView
在应用程序启动后弹出一个窗口。它工作正常,但我只希望弹出窗口出现在应用程序的第一次启动。目前,我在
applicationdFinishLaunching
方法的
AppDelegate
类中获得了
UIAlertView
。这是我的密码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
sleep(4);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome!" message:@"SAMPLE!!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

我是应用程序开发新手,如果这很简单,很抱歉。

请使用NSUserDefault,它最初设置为0,第一次显示警报后设置为1

//default value is 0
[settings registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:@"HAVE_SHOWN_REMINDER"]];
    BOOL haveShownReminder = [settings boolForKey:@"HAVE_SHOWN_REMINDER"];  
    if (!haveShownReminder)
{
    //show your alert

    //set it to 1
    [[NSUserDefaults standardUserDefaults]  setObject:[NSNumber numberWithInt:1] forKey:@"HAVE_SHOWN_REMINDER"];    
}

要确定应用程序是否第一次运行,您需要有一个持久变量来存储此信息。是存储这些简单配置值的最佳方式

比如说,

-(BOOL)application:(UIApplication *)application … {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    if (! [defaults boolForKey:@"notFirstRun"]) {
      // display alert...
      [defaults setBool:YES forKey:@"notFirstRun"];
    }
    // rest of initialization ...
}
这里,
[默认布尔工作:@“notFirstRun”]
从配置中读取名为
notFirstRun
的布尔值。这些值初始化为NO。因此,如果该值为NO,则执行
if
分支并显示警报


完成后,我们使用
[默认设置setBool:YES forKey:@“notFirstRun”]
将此布尔值更改为YES,这样
if
分支将不再执行(假设用户不删除该应用程序)。

由于上述解决方案在大多数情况下都会起作用,我想让您及时了解下面的内容

苹果的文档说:NSUserDefaults类目前不支持每台主机的首选项。为此,必须使用CFPreferences API。参考号


关于这一点的讨论

3条简单的线

-(BOOL)application:(UIApplication *)applicationBlahBlah {
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"NOT_FIRST_TIME"] boolValue])
    {
        [[[[UIAlertView alloc] initWithTitle:nil message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease] show];
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"NOT_FIRST_TIME"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

registerDefaults:
extra不是吗?这样做没有问题,但您可以直接使用bool设置标志。更容易认识到我正在处理NSNumber对象。不是一些布尔值
-(BOOL)application:(UIApplication *)applicationBlahBlah {
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"NOT_FIRST_TIME"] boolValue])
    {
        [[[[UIAlertView alloc] initWithTitle:nil message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease] show];
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"NOT_FIRST_TIME"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}