【问题标题】:Need Help With NSTimer and UISegmentedControl在 NSTimer 和 UISegmentedControl 方面需要帮助
【发布时间】: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


    【解决方案1】:

    如果按钮标签是“S...t”而不是“Start”,则需要将字体变小或调用adjustsFontSizeToFitWidth,例如

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

    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    

    【讨论】:

    • 嗯好的让我试试看,adjustsFontSizeToFitWidth 怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2014-02-06
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多