-1

I need to trigger an Alert after 60 seconds… My method, which you can find below, doesn't work… Please help. 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];

or i tried something like that instead of Timmer2 i wrote

_START.hidden = TRUE;

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

}

but it didn't work either………… Please Help! Thank you very much :-)

Julian E.

  • 3
    Please, try to name your method/var starting with a lower case. A `NSTimer` isn't a `"int"` value, it's an object, so you can't compare with `==60`. You better check this:`(countNumber>=60)` – Larme Apr 13 '14 at 16:39
  • doesn't work…… Still. I tried to exchange it, nothing happened at all – Juliane Kakoli Apr 13 '14 at 16:48

3 Answers3

2

You have to show the alert. Add [Alert show]; after initializing your alert.

Edit:

Other than the fact that you left out [Alert show] from your code, you need to force Timmer2 to run on the current run loop after its initialization in order to make changes to the UI, ex.

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

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

And another good post on this topic: NSTimer not firing when runloop is blocked

Community
  • 1
  • 1
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • where to add it? I added it to Alertus, didn't work. when I added it to the if statement,still din't work… – Juliane Kakoli Apr 13 '14 at 16:47
  • Yes, in alerts underneath the line of code already there. Why do you have ">" symbol throughout your code? Shouldn't be in any of the places where you have it written... – Lyndsey Scott Apr 13 '14 at 16:52
  • Oh, my bad… That was just formatting for Stackoverflow…. So what can i do @ Lyndsey Scott? It doesn't work under Alertus... – Juliane Kakoli Apr 13 '14 at 16:55
  • The action method of a timer is called on the *same thread* that was used to create the timer (in this case, the main thread). – Martin R Apr 13 '14 at 17:10
  • 1
    @MartinR Yeah, I updated my answer... Still working to iron out the explanation though. But I do think this code is the proper solution... – Lyndsey Scott Apr 13 '14 at 17:12
  • @LyndseyScott: You can start multiple timers from the main thread using `[NSTimer scheduledTimerWithTimeInterval:...` and they won't block each other. – Martin R Apr 13 '14 at 17:16
  • @MartinR I mentioned the timer, but I also said "any other actions running on your main thread." Maybe I should leave out the bit about the 2nd timer. I'm assuming though that since it's a game, the user may be manipulating the UI and blocking the run loop in doing so... – Lyndsey Scott Apr 13 '14 at 17:19
  • @user3527280 Could I see your latest code? Chat should be enabled soon since we've posted a lot of comments... Just reply to this message. – Lyndsey Scott Apr 13 '14 at 17:26
  • @user3527280: Are the timer functions (alertus, timmercount) called at all? – Martin R Apr 13 '14 at 17:27
  • It looks like there's a case-sensitivity mismatch when alertus and timmercount are called within the NSTimers... but it could just be a typo... I'd like to see the latest code. – Lyndsey Scott Apr 13 '14 at 17:29
  • @LyndseyScott How do you activate chat? – Juliane Kakoli Apr 13 '14 at 17:30
  • @user3527280 It's really annoying actually... Chat isn't activated until there are enough comments. I guess there still aren't enough... Is your TimerDisplay label updating properly at least? – Lyndsey Scott Apr 13 '14 at 17:31
  • @LyndseyScott how will it be activated? How will it look like? – Juliane Kakoli Apr 13 '14 at 17:33
  • It's not activated yet. It'll show up near the comment box. Maybe they removed the feature... – Lyndsey Scott Apr 13 '14 at 17:35
  • But is your TimerDisplay updating properly every second? – Lyndsey Scott Apr 13 '14 at 17:36
  • I'd like to know if your other timer is firing properly... Is your TimerDisplay updating as expected? – Lyndsey Scott Apr 13 '14 at 17:40
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];

_

// Above the Setup the Alert

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

}

I checked in a project in my own and everything runs perfectly. If you connect everything up, then after 30 seconds the Pop Up goes off, having a Dismiss button. It looks pretty nice! I hope that helped you! Andrew

0

i guess you missed to show the alert. add

[Alert show];

Use the following code

define 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;
   }
}
Community
  • 1
  • 1
karthikPrabhu Alagu
  • 3,371
  • 1
  • 21
  • 25
  • i think u have not implemented handleTimer method – karthikPrabhu Alagu Apr 13 '14 at 17:03
  • No i copied the exact code as you have written and the error described above comes at -(void) HandleTimer: (Id) Sender – Juliane Kakoli Apr 13 '14 at 17:13
  • -(void)StartGame { NSTimer *myTimer; myTimer = [NSTimer scheduledTimerWithTimeInterval: 10 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]; -(void)handleTimer: (id) sender { [ [[UIAlertView alloc]initWithTitle:@"GAME OVER" message:@"Thank you for Playing!" delegate:self cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil] show]; } -(void)stopTimer: (id) sender { if(myTimer) { [myTimer invalidate]; myTimer = nil; } }} -(void)TimmerCount{ Countnumber = Countnumber + 1; TimerDisplay.text = [NSString stringWithFormat:@"%i",Countnumber]; } – Juliane Kakoli Apr 13 '14 at 17:17
  • check NSTimer example http://webbuilders.wordpress.com/2009/04/10/timer-example-using-nstimer/ and UIAlertView example http://www.appcoda.com/hello-world-build-your-first-iphone-app/ – karthikPrabhu Alagu Apr 13 '14 at 17:51