Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-29-2011, 09:37 PM   #1 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Timer Problem how to invalidate

Hello again, another problem that seemed simple but is driving me bananas.

I have a timer that counts down -1 over a number of views. It works fine the first time, but if I return to the first view, the timer counts down -2. if I do this a third time it counts down -3 and so on.

I need to know how to reset the timer.

I think I have to use invalidate and an if statement, but I just can't work out where to put it?

Any advice?

Here is my .h

Code:
#import <UIKit/UIKit.h>
#import "PicPuzzleAppDelegate.h"
#import "puzzle2.h"


@interface puzzle1 : UIViewController {

    IBOutlet UILabel *theScore;
    IBOutlet UILabel *theTime;
    NSInteger counter;
    
}

@property (retain, nonatomic) UILabel *theScore;
@property (retain, nonatomic) UILabel *theTime;
@property (nonatomic, readwrite) NSInteger counter;


-(IBAction)correct1;
-(IBAction)HomeButton;
-(IBAction)pushforward;



@end
and here is part of my .m code ( I think the problem lies in this section)

Code:
- (void)viewDidLoad

{ //CHECK TIMer?
    
    PicPuzzleAppDelegate *PicPuzzle = (PicPuzzleAppDelegate *)[[UIApplication sharedApplication]delegate];
    
    //set timer
    PicPuzzle.minutes = 5.00;
    PicPuzzle.seconds = 0.00;
    
	// Start the timer for the countdown
	PicPuzzle.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    
    //set score to 0
    PicPuzzle.data = 0;
    [super viewDidLoad];
    }

-(void)countDown
{
    PicPuzzleAppDelegate *PicPuzzle = (PicPuzzleAppDelegate *)[[UIApplication sharedApplication]delegate];
	// Question live counter

    {
    
        theTime.text = [NSString stringWithFormat:@"%i:%2i",PicPuzzle.minutes, PicPuzzle.seconds];
        
        
        if (PicPuzzle.seconds >= 1) { 
            PicPuzzle.seconds = PicPuzzle.seconds - 1;
        }
        
    else {
            PicPuzzle.minutes = PicPuzzle.minutes - 1;
            PicPuzzle.seconds = 59;
            
            }
       }
    
    if(PicPuzzle.minutes == 0 && PicPuzzle.seconds ==0) 
        {
            puzzleEnd *screen = [[puzzleEnd alloc] initWithNibName:nil bundle:nil];
            screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            [self presentModalViewController:screen animated:YES];
            
            [screen release];
        }
        
      //  [self pushforward];}
}
I've tried putting:
Code:
/* if(PicPuzzle.timer){
        [PicPuzzle.timer invalidate];
        PicPuzzle.timer = nil;*/
but not sure where, and it won't work.

Can someone show me what I'm doing wrong?

Cheers guys.

Marshu
marshusensei is offline   Reply With Quote
Old 04-29-2011, 10:02 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Set up the timer in viewWillAppear and invalidate in viewWillDisappear.
Domele is offline   Reply With Quote
Old 04-29-2011, 11:19 PM   #3 (permalink)
Registered Member
 
Goldoneh's Avatar
 
Join Date: Nov 2009
Location: ایران
Posts: 45
Goldoneh is on a distinguished road
Default

You have one counter start from 5min to down, So you should check timer when receive to 00:00, you show alert and invalidate the timer.
__________________
The fantastic free iphone/ipod game:
iWarShip

iMasterMind
Goldoneh is offline   Reply With Quote
Old 04-30-2011, 12:04 AM   #4 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default thanks kazbluesky

thanks for you advice guys.

It works for the first view, but if I exit on the 2nd view and restart, the timer takes -2.

Hmm.
marshusensei is offline   Reply With Quote
Old 04-30-2011, 12:46 AM   #5 (permalink)
Student
 
Join Date: Mar 2011
Location: Canada
Posts: 107
pure is on a distinguished road
Default

Quote:
Originally Posted by marshusensei View Post
thanks for you advice guys.

It works for the first view, but if I exit on the 2nd view and restart, the timer takes -2.

Hmm.
How About trying to add 2 second to the second view that may help
pure is offline   Reply With Quote
Old 04-30-2011, 01:06 AM   #6 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Thanks

thanks for all your help guys. I really appreciate this board. I post a question, and no matter how stupid or simple it is, someone on here responds.
I hope to become more involved in this forum community.

I have learnt so much from others on here, and hope that soon I will be able to offer the same kind of advice that has been given me.

I got the timer working without using invalidate at all...
I simply created a second "selector" and used this with an if statement. Works, but I have a feeling this is a "dirty" method.
marshusensei is offline   Reply With Quote
Old 04-30-2011, 01:24 AM   #7 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

One question, by counting down -1, you mean it goes 5 minutes, 4 minutes 59 seconds, 4 minutes 58 seconds, etc. right? And -2 it goes down by 2 seconds right? If so I have an idea on how to solve your problem. This is assuming your timer is never canceled manually and is only canceled on its own when it reaches 0.

Code:
if (PicPuzzle.timer == nil) {
//start and configure timer
}
Domele is offline   Reply With Quote
Reply

Bookmarks

Tags
count down, invalidate, nstimer, reset

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
11 members and 322 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, Nobbsy, oztemel, pbart, PlutoPrime, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:55 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0