【问题标题】:Objective-C view pop-up after random time随机时间后的Objective-C视图弹出
【发布时间】:2013-11-22 14:13:26
【问题描述】:

假设 John 使用我的应用程序 3-6 分钟。 然后我想弹出一个视图,在我的情况下,包括一个广告。

这样的,

 AdViewController *adViewController = [[AdViewController alloc] init];
 [self presentViewController:adViewController animated:YES completion:nil];

但是我怎样才能让它在随机时间后弹出呢?我想我必须使用委托文件并使用 arc4random 函数。

约翰看过广告后,他必须关闭它,但这不是问题..

谁能给我一个代码示例?

【问题讨论】:

  • 你用谷歌搜索过这个吗?到目前为止,您尝试了哪些代码来实现目标?
  • 所以您有一个想法,我建议您尝试实现它,编写一些代码,如果您仍然遇到问题,请提出更具体的问题。

标签: ios objective-c random ads arc4random


【解决方案1】:

简单的解决办法是

  • 创建一个 NSTimer 并让它每 300 秒(5 分钟)触发一次
  • NSTimer 将触发一个显示您的弹出窗口的操作。

我不明白为什么这么难理解?

//use arc4random() if you need random time


NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:@selector(rateThisApp) userInfo:nil repeats:YES];

// *********
// ********* RATE APP ***********
// *********
- (IBAction)rateThisApp
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate this App"
                                                                message:@"Are you enjoying this app? Please leave a rating at the app store and tell us what you think of this app and its features. We would love to hear from you!"
                                                               delegate:self cancelButtonTitle:@"Not Now"
                                                      otherButtonTitles:@"Rate Now", nil];
        [alert show];
        alert.tag = 400;

}


-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 400)
    {
        if (buttonIndex == 0)
        {
            //dont do anything, user hit cancel
        }
        else if (buttonIndex == 1)
        {
            [[UIApplication sharedApplication]
             openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1234567"]];
        }
    }

}

【讨论】:

  • 非常感谢。我显然没有想到objective-c中已经有一个内置的计时器。但我仍然对 -5(或更多)感到困惑!
  • 这里的大多数开发人员都非常乐于助人,但他们希望您在发布问题之前尝试代码、研究、谷歌、阅读。这就是为什么大多数人都对你的问题打分了。
  • 我明白了。谢谢,下次我会改进我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 2017-05-14
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多