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

NSTimer/NSTimeInterval
EN

Stack Overflow用户
提问于 2012-05-05 03:19:04
回答 1查看 1.1K关注 0票数 0

我正在尝试制作一个从30倒计时到0的计时器,但这是我唯一能想到的让它工作的方法,但它不起作用。有人知道我做错了什么吗?

.h文件

代码语言:javascript
复制
@interface countDownAppViewController : UIViewController {

UIButton *countDown;
UILabel *displayThis;
}

@property (nonatomic, retain) IBOutlet UIButton *countDown;
@property (nonatomic, retain) IBOutlet UILabel *displayThis;

-(IBAction) theCount:(id) sender;
-(IBAction) displayStuff:(id) sender;

@end

.m文件

代码语言:javascript
复制
@synthesize countDown;
@synthesize displayThis;

-(IBAction) theCount:(id) sender    {
[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(displayStuff:)
                               userInfo:nil
                                repeats:NO];

}
int batman=30;
-(void) viewDidLoad{

displayThis.text = [NSString stringWithFormat:@"%i",batman];
}

-(IBAction) displayStuff:(id) sender    {
while (batman >= 0){
    batman--;
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(displayStuff:)
                                   userInfo:nil
                                    repeats:NO];
displayThis.text = [NSString stringWithFormat:@"%i",batman];

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-05 03:34:09

你有没有试过按照实际应该写的方式来写?repeats参数正是为了这个目的而存在的。你可以像这样写一个方法:

代码语言:javascript
复制
@interface Whatever: UIViewController
{
    NSTimer *timer;
    int count;
    int maxCount;
}

- (void)countDownFrom:(int)cnt;

@end

@implementation Whatever

- (void)countDownFrom:(int)cnt
{
    maxCount = cnt;
    count = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(doCount)
                               userInfo:nil
                                repeats:YES];
}

- (void)doCount
{
    count++;
    textField.text = [NSString stringWithFormat:@"Count: %d", count];
    if (count >= maxCount)
    {
        [timer invalidate];
    }
}

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

https://stackoverflow.com/questions/10454881

复制
相关文章

相似问题

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