【问题标题】:Convert NSTimer to UILabel-string?将 NSTimer 转换为 UILabel 字符串?
【发布时间】:2013-08-26 20:55:13
【问题描述】:

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

}

【问题讨论】:

  • 您不想“将 NSTimer 转换为 UILabel”。您想显示剩余时间 UILabel。两者之间存在巨大差异。

标签: ios uilabel nstimer nstimeinterval


【解决方案1】:

您可以将标签设置为 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];
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2019-01-05
      • 2016-02-20
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多