xcode 如何让 UIAlertView 在 iPhone 应用程序第一次启动时只出现一次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2578820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a UIAlertView appear only once, at the first start-up of an iPhone app?
提问by user309245
I'm using the UIAlertView
to make a pop-up happen once the app has started up. It works fine but I only want the pop up to appear on the first start-up of the app. At the moment I've got the UIAlertView
in the AppDelegate
class, in applicationDidFinishLaunching
method. Here is my code:
UIAlertView
一旦应用程序启动,我就会使用来制作一个弹出窗口。它工作正常,但我只希望弹出窗口出现在应用程序的第一次启动时。目前我已经UIAlertView
在AppDelegate
课堂上,在applicationDidFinishLaunching
方法中。这是我的代码:
- (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];
I'm new to app development so sorry if this is simple.
我是应用程序开发的新手,如果这很简单,我很抱歉。
回答by kennytm
To determine whether the app is run the first time, you need to have a persistent variable to store this info. NSUserDefaultsis the best way to store these simple configuration values.
要确定应用程序是否是第一次运行,您需要有一个持久变量来存储此信息。NSUserDefaults是存储这些简单配置值的最佳方式。
For example,
例如,
-(BOOL)application:(UIApplication *)application … {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"notFirstRun"]) {
// display alert...
[defaults setBool:YES forKey:@"notFirstRun"];
}
// rest of initialization ...
}
Here, [defaults boolForKey:@"notFirstRun"]
reads a boolean value named notFirstRun
from the config. These values are initialized to NO. So if this value is NO, we execute the if
branch and display the alert.
在这里,[defaults boolForKey:@"notFirstRun"]
读取notFirstRun
从配置命名的布尔值。这些值被初始化为 NO。因此,如果此值为 NO,我们将执行if
分支并显示警报。
After it's done, we use [defaults setBool:YES forKey:@"notFirstRun"]
to change this boolean value to YES, so the if
branch will never be executed again (assume the user doesn't delete the app).
完成后,我们使用[defaults setBool:YES forKey:@"notFirstRun"]
将这个布尔值更改为 YES,这样if
分支将永远不会再次执行(假设用户没有删除应用程序)。
回答by zoul
- (void) displayWelcomeScreen
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *alreadyRun = @"already-run";
if ([prefs boolForKey:alreadyRun])
return;
[prefs setBool:YES forKey:alreadyRun];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"…"
message:@"…"
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
回答by NeilInglis
Use a NSUserDefault which is initially set to 0 and which you set to 1 after showing the alert for the first time.
使用初始设置为 0 的 NSUserDefault,并在第一次显示警报后设置为 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"];
}
回答by comonitos
3 simple lines
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];
}
}
回答by Saran
As above solution should work in most cases, i would like to keep you aware on below as well at this point in time.
由于上述解决方案在大多数情况下都应该有效,因此我想在此时让您了解以下内容。
Apple document says: The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API. Ref NSUserDefaults
Apple 文档说: NSUserDefaults 类目前不支持每个主机的首选项。为此,您必须使用 CFPreferences API。参考NSUserDefaults
And a discussion on this here
和关于这个的讨论here