首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTimer问题

NSTimer问题
EN

Stack Overflow用户
提问于 2010-07-11 04:14:44
回答 2查看 6.8K关注 0票数 3

因此,我正在尝试设置一个基本的计时器,但我失败得很痛苦。基本上我想要的是当用户点击一个按钮时启动一个60秒的计时器,并用剩余的时间更新一个标签(就像倒计时一样)。我创建了我的标签和按钮,并在IB中连接它们。接下来,我为按钮创建了一个IBAction。现在,当我试图根据计时器更新标签时,我的应用程序搞砸了。下面是我的代码:

代码语言:javascript
复制
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1
                      target: self
                      selector:@selector(updateLabelDisplay)
                      userInfo: nil repeats:YES];

我还有一个updateLabelDisplay函数,它可以确定计时器运行了多少次,然后从60中减去这个数字,并在倒计时标签中显示这个数字。有人能告诉我我哪里做错了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-11 05:04:18

好的,对于初学者来说,如果你还没有这样做,那就来看看这个:Official Apple Docs about Using Timers

根据您的描述,您可能需要如下所示的代码。我已经做了一些关于行为的假设,但你可以适应口味。

此示例假设您希望保留对计时器的引用,以便可以暂停该计时器或其他什么。如果不是这样,您可以修改handleTimerTick方法,使其接受NSTimer*作为参数,并在计时器到期后使用此参数使其无效。

代码语言:javascript
复制
@interface MyController : UIViewController
{
  UILabel * theLabel;

  @private
  NSTimer * countdownTimer;
  NSUInteger remainingTicks;
}

@property (nonatomic, retain) IBOutlet UILabel * theLabel;

-(IBAction)doCountdown: (id)sender;

-(void)handleTimerTick;

-(void)updateLabel;

@end

@implementation MyController
@synthesize theLabel;

// { your own lifecycle code here.... }

-(IBAction)doCountdown: (id)sender
{
  if (countdownTimer)
    return;


  remainingTicks = 60;
  [self updateLabel];

  countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(handleTimerTick) userInfo: nil repeats: YES];
}

-(void)handleTimerTick
{
  remainingTicks--;
  [self updateLabel];

  if (remainingTicks <= 0) {
    [countdownTimer invalidate];
    countdownTimer = nil;
  }
}

-(void)updateLabel
{
  theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}


@end
票数 11
EN

Stack Overflow用户

发布于 2010-09-28 10:39:45

现在发布这个问题的第二个答案可能有点晚了,但我一直在寻找一个好地方来发布我自己对这个问题的解决方案。如果它对这里的任何人都有用,它就是。它会触发8次,当然这也可以根据你的喜好进行定制。时间一到,定时器就会自动释放。

我喜欢这种方法,因为它使计数器与计时器集成在一起。

要创建实例,请调用类似以下内容的代码:

代码语言:javascript
复制
SpecialKTimer *timer = [[SpecialKTimer alloc] initWithTimeInterval:0.1 
                                                         andTarget:myObject
                                                       andSelector:@selector(methodInMyObjectForTimer)];

无论如何,下面是头文件和方法文件。

代码语言:javascript
复制
//Header

#import <Foundation/Foundation.h>

@interface SpecialKTimer : NSObject {

    @private

    NSTimer *timer;

    id target;
    SEL selector;

    unsigned int counter;

}

- (id)initWithTimeInterval:(NSTimeInterval)seconds
                 andTarget:(id)t
               andSelector:(SEL)s;
- (void)dealloc;

@end

//Implementation

#import "SpecialKTimer.h"

@interface SpecialKTimer()

- (void)resetCounter;
- (void)incrementCounter;
- (void)targetMethod;

@end

@implementation SpecialKTimer

- (id)initWithTimeInterval:(NSTimeInterval)seconds
                 andTarget:(id)t
               andSelector:(SEL)s {

    if ( self == [super init] ) {

        [self resetCounter];

        target = t;
        selector = s;

        timer = [NSTimer scheduledTimerWithTimeInterval:seconds
                                                 target:self
                                               selector:@selector(targetMethod)
                                               userInfo:nil
                                                repeats:YES];

    }

    return self;

}

- (void)resetCounter {

    counter = 0;

}

- (void)incrementCounter {

    counter++;

}

- (void)targetMethod {

    if ( counter < 8 ) {

        IMP methodPointer = [target methodForSelector:selector];
        methodPointer(target, selector);

        [self incrementCounter];

    }

    else {

        [timer invalidate];
        [self release];

    }

}

- (void)dealloc {

    [super dealloc];

}

@end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3220695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档