1

好的,所以当用户第一次启动我的应用程序时,我想要一个警报视图,它会弹出 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];
}

当我第一次启动应用程序时,没有弹出警报视图。难道我做错了什么?顺便说一句,我希望它仅在应用程序首次启动时显示。它不得显示任何其他时间。提前致谢。

4

3 回答 3

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 中。

于 2012-07-04T10:03:09.240 回答
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];
    }
}
于 2012-07-04T09:20:05.723 回答
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
         }
}
于 2012-07-04T09:24:27.190 回答