繁体   English   中英

在 NSTimer 和 UISegmentedControl 方面需要帮助

[英]Need Help With NSTimer and UISegmentedControl

我有以下适用于 Timer 视图的代码。

问题是,当我更改段时,定时器已经运行,label 重置,但按钮文本仍然停止,我需要它保持“开始”。 我尝试通过执行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;
}

如果按钮 label 读取“S...t”而不是“开始”,那么您需要使字体更小或调用adjustsFontSizeToFitWidth ,例如

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

或者

button.titleLabel.adjustsFontSizeToFitWidth = YES;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM