Вопрос

I am trying to implement a NSTimer counting down from 5 seconds. But I have not been able to convert a NSTimer to a UILabel counting from 5 to 0. What steps should I take from here?

Regards

MTPopupWindow.h

@class MTPopupWindow;

..............


@property (nonatomic, retain) NSTimer* timer;
@end

MTPopupWindow.m

@interface MTPopupWindow() <UIWebViewDelegate>
{
UIView* _dimView;
UIView* _bgView;
UIActivityIndicatorView* _loader;
NSTimer *timer;

}


@implementation MTPopupWindow

@synthesize fileName = _fileName;
@synthesize webView = _webView;
@synthesize usesSafari = _usesSafari;
@synthesize delegate = _delegate;
@synthesize timer;

-(void)showInView:(UIView*)v
{

self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];  

progress = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
progress.textColor=[UIColor redColor];
progress.text = [NSString stringWithFormat:@"%@",timer];
[self addSubview: progress];


}

-(void)timerFireMethod:(NSTimer *)theTimer{

NSLog(@"bla bla time is out");
MTPopupWindowCloseButton* btnClose = [MTPopupWindowCloseButton buttonInView:self];
[btnClose addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];

}
Это было полезно?

Решение

You could set your label to 5 and have the timer fire every 1 second instead of 5 and set repeat to YES to make it fire every second.

Then in your method that the timer fires, have it decrement the label by 1 each time (would be easiest if you have an int variable, but you could do it with just the label). Then, when the label is at 0, you can stop the timer and call your real timer method.

For example:

-(void)showInView:(UIView*)v {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];  

    self.progress = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
    self.progress.textColor=[UIColor redColor];
    self.progress.text = @"5";
    [self addSubview: self.progress];


}

-(void)timerFireMethod:(NSTimer *)theTimer{
    int time = [self.progress.text intValue];
    time--;
    [self.progress setText:[NSString stringWithFormat:@"%@",time]];
    if (time==0) {
        [self.timer invalidate];
        NSLog(@"bla bla time is out");
        MTPopupWindowCloseButton* btnClose = [MTPopupWindowCloseButton buttonInView:self];
        [btnClose addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
    }
}

Другие советы

You'll want to change the time interval of your timer to 1.0 instead of 5. That way, the timerFireMethod will be called every second, and you'll be able to update your label accordingly. You might also want an NSUInteger variable to keep track of the elapsed seconds. Something like this:

@implementation ViewController{
    NSUInteger totalSeconds;
}

-(void)showInView:(UIView*)v
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];  

    progress = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
    progress.textColor=[UIColor redColor];
    progress.text = [NSString stringWithFormat:@"%@",timer];
    [self addSubview: progress];
}

-(void)timerFireMethod:(NSTimer *)theTimer
{
   if(elapsedSeconds < 1){
       [self.timer invalidate];
       NSLog(@"bla bla time is out");
       MTPopupWindowCloseButton* btnClose = [MTPopupWindowCloseButton buttonInView:self];
       [btnClose addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
       return;
   }

   elapsedSeconds--;
   progress.text = [NSString stringWithFormat:@"%d", elapsedSeconds];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top