如果Timer达到60秒,则必须弹出AlertView

时间:2014-04-13 16:37:57

标签: ios objective-c if-statement uialertview nstimer

我需要在60秒后触发警报...我的方法,你可以在下面找到,不起作用......请帮忙。 ViewController.m

-(IBAction)StartGame:(id)sender
{
    [self StartGame];
}

-(void) alertus
{
     Alert = [[UIAlertView alloc]initWithTitle:@"GAME OVER" message:@"Thank you for Playing!" >delegate:self cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil];

}

-(void)startGame
{
  Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimmerCount) >userInfo:nil repeats:YES];
    _START.hidden = TRUE;

    Timmer2 = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(Alertus) >userInfo:nil repeats:NO];
}

-(void)timmerCount{
   Countnumber = Countnumber + 1;
   TimerDisplay.text = [NSString stringWithFormat:@"%i",Countnumber];

或者我尝试过类似的东西 而不是Timmer2我写的

_START.hidden = TRUE;

if (Countnumber>=60) {
    [self Alertus];

}

但它也无效............ 请帮忙!非常感谢你: - )

Julian E。

3 个答案:

答案 0 :(得分:2)

您必须显示提醒。初始化警报后添加[Alert show];

编辑:

除了从代码中遗漏[Alert show]之外,您需要强制Timmer2在初始化后在当前运行循环上运行,以便对UI进行更改,例如。

[[NSRunLoop mainRunLoop] addTimer:Timmer2 forMode:NSRunLoopCommonModes];

Here's a better explanation of why than I could provide.

关于这个主题的另一篇好文章:NSTimer not firing when runloop is blocked

答案 1 :(得分:1)

-(IBAction)Start:(id)sender
{
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(TimerCount) userInfo:nil repeats: YES];
_STARTBUTTON.hidden = TRUE;
Timmer2 = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(Alertus) userInfo:nil repeats:NO];


}


-(void) Alertus
 {

// The Actual Alert
Alert = [[UIAlertView alloc]initWithTitle:@"GAME OVER" message:@"Thank you for Playing!"   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];

_

//设置警报

上方
[Alert show];
[Timer invalidate];
Timer = nil;

}

我自己检查了一个项目,一切都运行得很好。如果你把所有东西连接起来,那么30秒后弹出窗口就会关闭,有一个Dismiss按钮。它看起来很漂亮!我希望能帮到你! 安德鲁

答案 2 :(得分:0)

我猜你错过了显示警报。 添加

[Alert show];

使用以下代码

定义kTimeInSeconds 1

 NSTimer *myTimer;
 myTimer = [NSTimer scheduledTimerWithTimeInterval: kTimeInSeconds 
                                   target:self 
                                   selector:@selector(handleTimer:) 
                                   userInfo:nil
                                   repeats:YES];


-(void)handleTimer: (id) sender 
{       
 UIAlertView * Alert =   [[UIAlertView alloc]initWithTitle:@"GAME OVER" message:@"Thank you for Playing!" delegate:self cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil];
[Alert show];
}

-(void)stopTimer: (id) sender 
{       
   if(myTimer)
   {
       [myTimer invalidate];
        myTimer = nil;
   }
}