尝试做一个简单的倒计时应用程序但计数器没有倒计时?
Trying to do a simple countdown application but counter isn't counting down?
我正在研究 Xcode 6 并尝试制作一个简单的倒计时应用程序。
我的应用程序真的很简单。 UI 有一个标签和一个按钮。单击按钮时,它应该从 10 开始倒计时。
这是我的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSInteger count;
NSTimer *timer;
}
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)start {
count = 10;
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
};
-(void)timerFired:(NSTimer *)timer {
count -=1;
self.timerLabel.text = [NSString stringWithFormat:@"%i",count];
if (count == 0) {
[timer invalidate];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
程序编译正常,但是当我点击按钮开始倒计时时没有任何反应。我哪里做错了?
编辑:非常感谢你们!问题解决了。我花了 3 个小时只是想弄清楚我做错了什么才发现这是一个愚蠢的错误。啊。
爱你计算器!
你应该使用这个 NStimer 初始方法。
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
您代码中的主要问题是,
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
没有将你的计时器添加到runloop,所以你的计时器在一个invocation.And scheduledTimerWithTimeInterval 方法为你处理runloop 事情后立即释放。
在您的代码中替换此行
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
用这条线
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
您已经正确设置了触发方式,但计时器并未真正触发。作为 , you want to use the scheduledTimerWithTimeInterval
constructor as opposed to the timerWithTimeInterval
构造函数,除非您专门调用 [NSTimer fire]
.
,否则不会触发
因此,为此只需切换旧的构造函数即可:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
然后计时器应该正确触发(每秒)并调用关联的方法:
我正在研究 Xcode 6 并尝试制作一个简单的倒计时应用程序。 我的应用程序真的很简单。 UI 有一个标签和一个按钮。单击按钮时,它应该从 10 开始倒计时。
这是我的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSInteger count;
NSTimer *timer;
}
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)start {
count = 10;
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
};
-(void)timerFired:(NSTimer *)timer {
count -=1;
self.timerLabel.text = [NSString stringWithFormat:@"%i",count];
if (count == 0) {
[timer invalidate];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
程序编译正常,但是当我点击按钮开始倒计时时没有任何反应。我哪里做错了?
编辑:非常感谢你们!问题解决了。我花了 3 个小时只是想弄清楚我做错了什么才发现这是一个愚蠢的错误。啊。 爱你计算器!
你应该使用这个 NStimer 初始方法。
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
您代码中的主要问题是,
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
没有将你的计时器添加到runloop,所以你的计时器在一个invocation.And scheduledTimerWithTimeInterval 方法为你处理runloop 事情后立即释放。
在您的代码中替换此行
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
用这条线
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
您已经正确设置了触发方式,但计时器并未真正触发。作为 scheduledTimerWithTimeInterval
constructor as opposed to the timerWithTimeInterval
构造函数,除非您专门调用 [NSTimer fire]
.
因此,为此只需切换旧的构造函数即可:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
然后计时器应该正确触发(每秒)并调用关联的方法: