2011-07-22 70 views
1

我有以下代码适用于计时器视图。需要帮助NSTimer和UISegmentedControl

问题是,当我改变细分时,当计时器已经运行时,标签重置,但按钮文本仍然停留,我需要它保持“开始”。我试着手动做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; 
} 

回答

0

如果按钮标签读取“S ... T”,而不是“开始”,然后你需要的字体较小或调用adjustsFontSizeToFitWidth,例如

button.titleLabel.font = [UIFont systemFontOfSize: 10]; 

button.titleLabel.adjustsFontSizeToFitWidth = YES; 
+0

嗯,好吧,让我试试,我该怎么办'adjustsFontSizeToFitWidth'? – Jon