2013-12-13 77 views
0
@interface Taxi_MainViewController : Taxi_BaseNavViewController 
{ 
    __block NSTimer *timer_; 
} 



    [Taxi_do_order psg_place_orderWithMemberId:strMemberId_ orderStatus:K_orderStatus_open andCLLocationCoord:location.coordinate callback:^(NSInteger iOrderId){ 
     [Taxi_StatusView dismiss]; 
     if (iOrderId >=0) 
     { 
      isOrderExist = YES; 
      [weakSelf_ showWaittingDriverAcceptView]; 
      timer_ = [NSTimer scheduledTimerWithTimeInterval:2.0 
                   target:weakSelf_ 
                  selector:@selector(actListen:) 
                  userInfo:nil 
                   repeats:YES]; 
     }else 
      [weakSelf_ hideWaittingDriverAcceptView]; 
    }faile:^(){ 
     [Taxi_StatusView showLostNetWork]; 
    }]; 

當我調用[timer_ invalidate],timer_ = nil;在其他方法中,定時器功能仍然每2秒呼叫一次。如何在定時器內創建定時器時啓動定時器?

回答

1

在你的情況下,變量timer_永遠不會保留,並且在結束塊範圍處丟失指針 。擁有財產你有保留/釋放機制。

試試這個:

@interface Taxi_MainViewController : Taxi_BaseNavViewController 
{ 

} 

@property (nonatomic,retain) NSTimer* timer; 

@end 



[Taxi_do_order psg_place_orderWithMemberId:strMemberId_ orderStatus:K_orderStatus_open andCLLocationCoord:location.coordinate callback:^(NSInteger iOrderId){ 
     [Taxi_StatusView dismiss]; 
     if (iOrderId >=0) 
     { 
      isOrderExist = YES; 
      [weakSelf_ showWaittingDriverAcceptView]; 
      self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                   target:weakSelf_ 
                  selector:@selector(actListen:) 
                  userInfo:nil 
                   repeats:YES]; 
     }else 
      [weakSelf_ hideWaittingDriverAcceptView]; 
    }faile:^(){ 
     [Taxi_StatusView showLostNetWork]; 
    }];