[resolved]How to stop NSTimer?

Discussion in 'iOS Programming' started by alexandergre, Aug 6, 2009.

  1. alexandergre macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #1
    Code:
    -(IBAction)StartTheTimer:(id)sender{
    	[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    }
    
    - (void)updateCounter:(NSTimer *)theTimer {
    	static int count = 5;
    	count--;
    	NSString *s = [[NSString alloc]
    				   initWithFormat:@"%d", count];
    	timeLeft.text = s;
    	[s release];
    	if(count==0)
    	{
    		timeLeft.text=@"done";
    		//[COLOR="Red"][NSTimer invalidate];[/COLOR]
    	}
    	
    
    	
    }
    If I uncomment [NSTimer invalidate] Then I get a warning. :(
    : warning: 'NSTimer' may not respond to '+invalidate'
     
  2. alexandergre thread starter macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #2
    I figured it out:
    Code:
    -(IBAction)StartTheTimer:(id)sender{
    	[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    }
    
    - (void)updateCounter:(NSTimer *)theTimer {
    	static int count = 10;
    	count--;
    	NSString *s = [[NSString alloc]
    				   initWithFormat:@"%d", count];
    	timeLeft.text = s;
    	[s release];
    	if(count==0)
    	{
    		[theTimer invalidate];
    		timeLeft.text=@"done";
    		
    	}
     

Share This Page