NSTimer上的重置按钮

时间:2013-11-08 16:31:47

标签: ios objective-c nstimer

我有一个NSTimer,我需要一个重置按钮,重置屏幕上的变量并停止该计时器,直到它被启动按钮启动。这是一些代码:

@implementation TimeController

int timeTick = 0;


NSTimer *timer;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    labelTime.text = @"0";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startTimer:(id)sender {
    [timer invalidate];
    timer= [NSTimer scheduledTimerWithTimeInterval:60.0 target:(self) selector:(@selector(tick)) userInfo:(nil) repeats:(YES)];
}

- (IBAction)resetTicktock:(id)sender {
    [timer invalidate];

}

-(void)tick{
    timeTick++;
    NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
    labelTime.text = timeString;



}


@end

提前致谢!

2 个答案:

答案 0 :(得分:2)

看起来你已经启动了一个停止计时器的功能。只需在那里重置变量。

- (IBAction)resetTicktock:(id)sender {
    [timer invalidate];
    labelTime.text = @"0";
}

将您的按钮链接到故事板中的此方法,以执行“触摸内部”操作。

答案 1 :(得分:0)

您需要重置timeTick和标签文本

- (IBAction)resetTicktock:(id)sender {
   [timer invalidate];
   labelTime.text = @"0";
   timeTick = 0;
}