UIAlertView多次显示

时间:2012-10-31 08:46:18

标签: objective-c uialertview

我在用户运行应用时显示了一个简单的UIAlertView。它有这样的结构:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Welcome!", "")
                                                message:NSLocalizedString(@"This is a welcome message.", "")                             
                                               delegate:nil
                                      cancelButtonTitle:@"OK" 
                                      otherButtonTitles: nil];
[alert show];
[alert release];

问题是,如何自定义它以显示每5次运行?

提前致谢;)

2 个答案:

答案 0 :(得分:2)

AppDelegate类中会有一个方法,如

- (void)applicationDidBecomeActive:(UIApplication *)application

现在在此方法中创建一个NSUSerdefaults并生成一个整数并递增该整数并将其保存在NSUserdefaults中

现在,每次应用程序启动时,都会调用该方法,并且整数将递增

现在在下面的方法中制作if条件

if(your integer which has nsuserdefaults >=5)
{
   your alertview
    again here make your nsinteger  to Zero which is stored in nsuserdefaults 
     your integer which has nsuserdefaults =0
} 

这是你的第二个问题的答案,每次5次app运行后。将弹出警报 让我知道它是否正常工作.. !!!! 快乐的编码!!!!

答案 1 :(得分:1)

您可以使用NSUserDefaults来存储关键AppRunCount的应用执行次数(您可以引入自己的密钥名称):

int runCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"AppRunCount"] + 1

[[NSUserDefaults standardUserDefaults] setInteger:runCount forKey:@"AppRunCount"];

if (runCount <= 5) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Welcome!", "")
                                            message:NSLocalizedString(@"This is a welcome message.", "")                             
                                           delegate:nil
                                  cancelButtonTitle:@"OK" 
                                  otherButtonTitles: nil];
    [alert show];
    [alert release];
}

您只需将上面的代码添加到viewDidLoad

即可