将NSTimer转换为UILabel字符串



我正在尝试实现一个从5秒开始倒计时的NSTimer。但我无法将NSTimer转换为从5到0计数的UILabel。从这里开始我应该采取什么步骤?

问候

MTPopup Window.h

@class MTPopupWindow;
..............

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

MTPopup Window.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];
}

您可以将标签设置为5,让计时器每1秒启动一次,而不是5,并将repeat设置为YES,使其每秒钟启动一次。

然后,在计时器触发的方法中,让它每次将标签递减1(如果你有一个int变量,这将是最简单的,但你可以只使用标签)。然后,当标签为0时,您可以停止计时器并调用真正的计时器方法。

例如:

-(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];
    }
}

您需要将计时器的时间间隔更改为1.0,而不是5。这样,timerFireMethod将每秒被调用一次,您将能够相应地更新标签。您可能还需要一个NSUInteger变量来跟踪经过的秒数。类似这样的东西:

@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];
}

最新更新