NSTimer detailed settings

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

NSTimer detailed settings 1:

NSTimer detailed settings 2: http://blog.csdn.net/davidsph/article/details/7899731

1 ,,,,,

Let me talk about my business needs, recently in a small project, need to use the timer function, the NSTimer class, period, there are some minor problems, but in the end, through their own efforts, finally did come out. I'd like to summarize, I study and understanding of the NSTimer class.

Not much to say, the first effect chart

The interface element is very simple, two UIButton start and pause, 20 start the countdown. The final result is, press the start button, the countdown timer operation, according to the time, pause button, stop the countdown. When the countdown for the 0 time, a dialog box appears, suggesting that the time has come to.

Business demand is very simple, but, in our implementation, but appeared, some small mistakes. Is mainly the pause button click the start button is not many, not click several times, I believe, just started, contact the NSTimer people, also can appear these problems.

Directly on the main code:

The controller class in the.H file

@interface sdsViewController : UIViewController<UIAlertViewDelegate>

//The definition of a timer, for instance variables

@property(nonatomic,retain) NSTimer *timer;

//Display the current state of the countdown

@property (retain, nonatomic) IBOutlet UILabel *timeDisplay;

//The start button, the response of action

- (IBAction)startTime:(id)sender;

//The pause button response action

- (IBAction)stopTime:(id)sender;

@end

Key code in.M

The start button response code:

- (IBAction)startTime:(id)sender {

 //If the timer object does not exist, create it and start

if(!_timer){

//Create a timer, this is directly added to the current message loop, note the distinction with other initialization method

 _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES]; 

//  [_timer fire]; //For the fire method, later will be detailed, it is not to start a timer, so simple

}

}

//Ending button response code:

- (IBAction)stopTime:(id)sender {

if (_timer) {

NSLog(@"Call self.time is true!!");

//If the timer running

if ([self.timer isValid]) {

NSLog(@"Click the stop button, cancel the timer!!");

[self.timer invalidate];

 //This code is the key

_timer=nil;

}

}

}

All OK, now the key procedures used in the analysis of local.

Have a look first class NSTimer, relatively simple

Tasks

Creating a Timer

//Create a timer, the constructor method is convenient, all know

+ scheduledTimerWithTimeInterval:invocation:repeats:

+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

+ timerWithTimeInterval:invocation:repeats:

+ timerWithTimeInterval:target:selector:userInfo:repeats:

//Initialization method

– initWithFireDate:interval:target:selector:userInfo:repeats:

//A timer is started, I'm afraid not so simple? ? ? ? ? ? ? ?

Firing a Timer

– fire

//Is the suspension of a timer, NO, Stop, written in very clear

Stopping a Timer

– invalidate

//The following information on the timer

Information About a Timer

– isValid  //Whether it is running

– fireDate //Returns the date at which the receiver will fire.

– setFireDate: //To set the timer running time

– timeInterval  //Timer delay time

– userInfo //Other information

------------------------------------------------------

2,,,,,,,,,

First, the initialization method

+ scheduledTimerWithTimeInterval:invocation:repeats:

+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

The two is to create a timer, and added to the current running cycle, we can to understand this, it initializes a timer (NSTimeInterval), in seconds time, automatically start the timer.

And the following two initialization method which is not the same:

+ timerWithTimeInterval:invocation:repeats:

+ timerWithTimeInterval:target:selector:userInfo:repeats:

The two is also created, but did not join, operation cycle. Class method to create the timer object without scheduling it on a run loop. then, After the establishment of, Must (after creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.), this is difference with the above two methods.

  //Creates a timer

_timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

//Must be manually added to the cycle of

NSRunLoop *runloop=[NSRunLoop currentRunLoop];

[runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

The above code is the same at both ends

On this method:

Firing a Timer

– fire

In fact, he wasn't really starts a timer, from initialization method before we can also see, established when, at the appropriate time, the timer will automatically start. This method is doing.

You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

This is the official document version, English said very clearly, but our understanding is not in place, in order to thoroughly understand its function. I had a test.

Also is very simple, the timer above, change a bit

//When the initialization creates a timer

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

 //Creates a timer,

_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

//Manual into circulation

NSRunLoop *runloop=[NSRunLoop currentRunLoop];

 [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

//Of course, this timer will start automatically, only not more than ten seconds, to trigger

}

return self

}

When we click the start button ” “,

- (IBAction)startTime:(id)sender {

//Simply calls this method, see what function

  [_timer fire];

}

As a result, click on the button, the countdown from 1, click on the reduction of 1, namely the trigger time to advance, but after ten seconds countdown is further reduced by 1, which is just ahead of the trigger timer, and the timer does not influence before the set time, like we can't wait to go. Watch a ball game, quickly put the car faster, fire functions as let us quickly to the stadium, but does not affect the game to start time.

Remember before the timer initialization, is set to YES, when we, to NO, which does not make it a cycle trigger, we then click start button. Will suddenly find, countdown minus 1, but when we click on the start button, will find the countdown, not moving. The reason is: our timer, is set to trigger only once, when fire, the trigger time, the timer, it is automatically destroyed, then fire will not trigger.

Again we see the official explanation now, may more understand,

You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

That's right, fire does not start a timer, just ahead of the trigger.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lynn at November 24, 2013 - 10:28 PM