好的,当用户第一次启动我的应用程序时,我想要一个警报视图,它会弹出2个选项。我正在使用以下方法:
- (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];
}
首次启动应用时,不会弹出警报视图。难道我做错了什么?顺便说一句,我希望它只在应用程序的首次启动时显示。它不能显示任何其他时间。提前致谢。
答案 0 :(得分:1)
试试这个:
(void)viewDidLoad{
NSLog(@"In viewDidLoad");
[self displayWelcomeScreen];
}
在displayWelcomeScreen中编写代码:
- (void) displayWelcomeScreen
{
NSLog(@"In 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];
}
并检查控制台是否显示在displayWelcomeScreen。
答案 1 :(得分:0)
尝试使用这些。我刚刚对你的代码进行了一些修改。
- (void) displayWelcomeScreen
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:@"alreadyRun"])
{
return;
}
else
{
[prefs setObject:@"YES" forKey:@"alreadyRun"];
[prefs synchronize];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"…"
message:@"…"
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
}
答案 2 :(得分:0)
在appDelegate didFinishLaunchingWithOptions中,添加这样的内容
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:@"everLaunched"]) {
[defaults setBool:YES forKey:@"everLaunched"];
[defaults setBool:YES forKey:@"firstLaunch"];
}
else{
[defaults setBool:NO forKey:@"firstLaunch"];
}
检查是否需要显示欢迎信息:
- (void) displayWelcomeScreen
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
{
NSLog(@"I should display alert view now with two selections");
//what to you want to to here now? show alert view? with two choices?
//you might want to remember the selected choice from user
}
else {
NSLog(@"the user already made his choice, this is not a first launch");
//what do you want to do here now? show home screen? let the user use the app?
//the user has already made his choice
}
}