由于项目中有很多一样的代码,所以写成一个函数封装起来
如下:
///头文件中
@property (retain, nonatomic) NSTimer *self.countDownTimer;
@property  NSInteger bjl_Countdown1;
///////////////////////////////
- (void)setBetTimer:(NSTimer *)timer
{
 self.bjl_Countdown1 = 35;
if(timer == nil)
        timer = [NSTimer scheduledTimerWithTimeInterval:(1) target:self selector:@selector(responseTimer) userInfo:nil repeats:YES];
  if(self.countDownTimer != nil){
      NSLog(@"创建倒计时成功");
  }

- (void)responseTimer
{
    if (bjl_Countdown1 == 0) {
 
        if (self.countDownTimer != nil && [self.countDownTimer isValid]) {
            NSLog(@"关闭倒计时");
            [self.countDownTimer invalidate];
            self.countDownTimer = nil;
        }
         
    }else{
        bjl_Timer1.text = [NSString stringWithFormat:@"%d",bjl_Countdown1];//将倒计时显示出来
        self.bjl_Countdown1 --;    }
}//执行调用
[self setBetTimer:self.countDownTimer ];执行完这一句以后,倒计时创建成功,NSLog(@"创建倒计时成功");这一句有打印但是当倒计时执行完毕bjl_Countdown1等于0的时候,NSLog(@"关闭倒计时");这一句却没有执行,countDownTimer已经为nil了
,创建的时候是不为空的,奇怪的是执行完毕却为空了但是将[self setBetTimer:self.countDownTimer ];不以函数的方式执行,直接替换成代码
self.bjl_Countdown1 = 35;
if(self.countDownTimer == nil)
        self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:(1) target:self selector:@selector(responseTimer) userInfo:nil repeats:YES];
  if(self.countDownTimer != nil){
      NSLog(@"创建倒计时成功");
  }
当倒计时为0的时候,NSLog(@"关闭倒计时")这一句会打印请教大家这两种方式有什么却别吗,怎么过一段时间countDownTimer就自动的变为空值了

解决方案 »

  1.   

    ///头文件中
    @property (retain, nonatomic) NSTimer *self.countDownTimer;
    这里不要加self.
      

  2.   

    执行调用 [self setBetTimer:self.countDownTimer ];
    此句,countDownTimer作为局部变量传入时,本身不占内存(上下文都未见有分配空间),默认就指向nil。
    “NSLog(@"创建倒计时成功");这一句有打印“ 这句话不知道你怎么打印出来的?解决方法:
    if(timer == nil)
            timer = [NSTimer scheduledTimerWithTimeInterval:(1) target:self selector:@selector(responseTimer) userInfo:nil repeats:YES];下面加上 self.countDownTimer = timer;就ok