1

次のコードは、Timerビューで使用できます。NSTimerとUISegmentedControlのヘルプが必要

問題は、セグメントを変更すると、タイマーが既に実行されている間にラベルがリセットされますが、ボタンのテキストは停止したままです。代わりに「開始」を維持する必要があります。 self.timer.titleLabel.text = @"Start";を手動で実行しようとしましたが、何らかの理由で"Start"の代わりに"S...t"と表示されます。

- (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

OKうーん、私は 'adjustsFontSizeToFitWidth'を行うのですかどのように、私はそれを試してみましょうか? – Jon

関連する問題