0

我正在尝试实现从 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];

}
4

2 回答 2

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];
    }
}
于 2013-08-26T21:04:21.827 回答
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];
}
于 2013-08-26T21:09:11.410 回答