iOS Nstimer (ii)

Source: Internet
Author: User

1. Stopping a timer off timer

If you create a non-repeating timer, there are no need to take any further action. It automatically stops itself after it fires. For example, there are no need to stop the timer created in the Initializing a timer with a fire Date. If you create a repeating timer, however, you stop it by sending it a invalidate  message. You can also send invalidate a non-repeating timer a message before it fires to prevent it from firing.

The above means that you can use the timer reference directly invalidate.

- (ibaction) stoprepeatingtimer:sender {    [self.repeatingtimer invalidate];     =- (ibaction) stopunregisteredtimer:sender {    [self.unregisteredtimer invalidate];     = Nil;}

You can also invalidate a timer from the method it invokes.

You can also invalidate the timer in its dispatch method, this will invalidate the timer after it has fired three times. Because the timer is passed as an argument to the method it invokes, there could be no need to maintain the timer a s a variable.

-(void) Countedtimerfiremethod: (nstimer*) thetimer {     *startdate = [[Thetimer userInfo] Objectforkey:@ "startdate"];    NSLog (@ "Timer started on%@; fire count%d", StartDate, self.timercount);     Self.timercount++    ; if 3 ) {        [thetimer invalidate];    }}

However, you might nevertheless keep a reference to the timer in case you want the option of stopping it earlier.

// The repeating timer is a weak property. @property (weak) Nstimer *Repeatingtimer;
*unregisteredtimer;

Keep a reference to the timer so that you can stop the timer in the place where you want it to be (invilidate).

iOS Nstimer (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.