如何使UIAlertView只出现一次,在iPhone应用程序的第一次启动? [英] How do I make a UIAlertView appear only once, at the first start-up of an iPhone app?
问题描述
我使用 UIAlertView
在应用程式启动后发出弹出式视窗。它工作正常,但我只想让弹出窗口出现在应用程序的第一次启动。目前我已经在 AppDelegate
类中的 applicationDidFinishLaunching 中找到
UIAlertView
code>方法。这是我的代码:
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:
- (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.
推荐答案
要确定应用程序是否是第一次运行,您需要有一个持久变量来存储此信息。 NSUser默认是这是存储这些简单配置值的最佳方式。
To determine whether the app is run the first time, you need to have a persistent variable to store this info. NSUserDefaults is the best way to store these simple configuration values.
例如,
-(BOOL)application:(UIApplication *)application … {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"notFirstRun"]) {
// display alert...
[defaults setBool:YES forKey:@"notFirstRun"];
}
// rest of initialization ...
}
这里, [defaults boolForKey:@notFirstRun]
从配置中读取一个名为 notFirstRun
的布尔值。这些值初始化为NO。所以如果这个值是NO,我们执行 if
分支并显示警报。
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 setBool:YES forKey:@notFirstRun]
将此布尔值更改为YES,因此 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).
这篇关于如何使UIAlertView只出现一次,在iPhone应用程序的第一次启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!