zoukankan      html  css  js  c++  java
  • iOS之 NSTimer(二)

    1. Stopping a Timer  关闭定时器

    if you create a non-repeating timer, there is no need to take any further action. It automatically stops itself after it fires. For example, there is 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 an invalidate message. You can also send a non-repeating timer an invalidate message before it fires to prevent it from firing.

    上面的就是说可以用timer的引用 直接 invalidate。

    - (IBAction)stopRepeatingTimer:sender {
        [self.repeatingTimer invalidate];
        self.repeatingTimer = nil;
    }
     
    - (IBAction)stopUnregisteredTimer:sender {
        [self.unregisteredTimer invalidate];
        self.unregisteredTimer = nil;
    }

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

    你也可以在它的调度方法中对timer 进行invalidate,  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 may be no need to maintain the timer as a variable. 

    - (void)countedTimerFireMethod:(NSTimer*)theTimer {
     
        NSDate *startDate = [[theTimer userInfo] objectForKey:@"StartDate"];
        NSLog(@"Timer started on %@; fire count %d", startDate, self.timerCount);
     
        self.timerCount++;
        if (self.timerCount > 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;
    @property (strong) NSTimer
    *unregisteredTimer;

    保持对timer的引用,这样你可以在你可以 在你想要的地方stop(invilidate) 这个timer。

  • 相关阅读:
    layout_weight使用
    Android sdk manager 下载速度慢的问题
    详谈OC(object-c)深浅复制/拷贝-什么情况下用retain和copy
    object-c(oc)内存管理机制详解
    xmpp实现的即时通讯聊天(二)
    xmpp实现的即时通讯聊天(一)
    iOS开发消息推送原理
    指定的转换无效。
    Oracle11g +Win 64+PLSQL9.0
    数据库连接字符串
  • 原文地址:https://www.cnblogs.com/Ohero/p/4832199.html
Copyright © 2011-2022 走看看