开发者

Need Help With NSTimer and UISegmentedControl

I have the following code that works for a Timer view.

The problem is, when I change segments, while the timer is already running, the label resets, but the button text still stay's stop, I need it to stay "start" instead. I tried doing it manually by doing self.timer.titleLabel.text = @"Start"; but for some reason it shows as "S...t" instead of "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.cou开发者_如何学编程nterSegment = 30;
    }
    else if (index == 2)
    {
        NSLog(@"60 sec");
        self.timerCount = @"60";
        self.counterSegment = 60;
    }
    self.counter = self.counterSegment;
    self.timerLabel.text = timerCount;
}


If the button label reads "S...t" instead of "Start", then you need to make the font smaller or invoke adjustsFontSizeToFitWidth, e.g.

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

or

button.titleLabel.adjustsFontSizeToFitWidth = YES;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜