将NSTimer转换为UILabel-string?

时间:2013-08-26 20:55:13

标签: ios uilabel nstimer nstimeinterval

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

此致

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

}

2 个答案:

答案 0 :(得分:3)

您可以将标签设置为5并让计时器每1秒触发一次而不是5秒,然后将重复设置为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)

您需要将计时器的时间间隔更改为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];
}