2011-07-22 100 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