2012-01-24 198 views
0

haii我有這樣的代碼在UISWitchControl用於激活自動滾動功能Geasture識別懷疑

-(IBAction)_clickautoscroll:(id)sender 
    { 
    if(switchcontrolautoscroll.on){ 

     if autoscrollTimer== nil) { 

      autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(55.0/1000.0) 
                   target:self 
                  selector:@selector(autoscrollTimerFired:) 
                  userInfo:nil 
                   repeats:YES]; 
     } 

    } 
    else{ 
     [switchcontrolautoscroll setOn:NO animated:YES]; 
     [autoscrollTimer invalidate]; 
     [self.table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 
    } 
    } 


- (void)autoscrollTimerFired:(NSTimer*)timer { 
    CGPoint scrollPoint = self.table.contentOffset; 
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); 
    [self.table setContentOffset:scrollPoint animated:NO]; 
} 

它工作正常,但我有長期的手勢識別器來結束自動滾屏的代碼是

-(void) handleLongPressOnUndoGesture:(UILongPressGestureRecognizer*)recognizer { 
    //[switchcontrolautoscroll setOn:NO animated:YES]; 
    [autoscrollTimer invalidate]; 
    [self.table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 
} 

但是當我點擊taptohold它會停止autoscroll,但應用程序崩潰。 在我的代碼中是否有任何缺陷。 謝謝。

回答

1

NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:創建一個自動釋放變量,並且不保留它。

讓autoscrollTimer一個保留的性質和使用self.autoscrollTimer

它可能已經被時間handleLongPressOnUndoGesture發佈被稱爲設置,而這也正是你崩潰。