【发布时间】:2011-07-22 09:42:26
【问题描述】:
我有以下适用于计时器视图的代码。
问题是,当我更改段时,当计时器已经运行时,标签会重置,但按钮文本仍然停止,我需要它保持“开始”状态。我尝试通过 self.timer.titleLabel.text = @"Start"; 手动执行此操作,但由于某种原因,它显示为 "S...t" 而不是 "Start"。
- (IBAction)startStop:(UIButton *)sender
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
[sender setTitle:@"Start" forState:UIControlStateNormal];
}
else
{
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
[sender setTitle:@"Stop" forState:UIControlStateNormal];
}
}
- (void)handleTimer:(NSTimer *)timer
{
self.counter--;
self.timerLabel.text = [NSString stringWithFormat:@"%ld", self.counter];
if ( self.counter <= 0 ){
[self.myTimer invalidate];
self.myTimer = nil;
}
}
- (IBAction)reset:(id)sender
{
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
}
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
if ( self.myTimer )
{
[self.myTimer invalidate];
self.myTimer = nil;
self.timer.titleLabel.text = @"Start";
}
if (index == 0)
{
NSLog(@"15 sec");
self.timerCount = @"15";
self.counterSegment = 15;
}
else if (index == 1)
{
NSLog(@"30 sec");
self.timerCount = @"30";
self.counterSegment = 30;
}
else if (index == 2)
{
NSLog(@"60 sec");
self.timerCount = @"60";
self.counterSegment = 60;
}
self.counter = self.counterSegment;
self.timerLabel.text = timerCount;
}
【问题讨论】:
标签: iphone objective-c cocoa-touch nstimer uisegmentedcontrol