Got an issue with my countdown timer who don't want to countdown from 60 and reset when 0 is reached. At the moment it just sets its label 0 and start counting down to -1, 2-... How do I get it to start from 60 on iOS in xcode?
我的倒数计时器出现了问题,他不想从60开始倒计时,并在达到0时重置。目前它只是将其标签设置为0并开始向下计数到-1,2 -...如何让它在xcode的iOS上从60开始?
.m file
.m文件
#import "ViewController.h"
@interface ViewController ()
{
int timeTick;
NSTimer *timer;
}
@end
@implementation ViewController
- (IBAction)stopStartBtn:(id)sender {
[timer invalidate];
timeTick = 3;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
}
-(void)myTicker{
timeTick--;
NSString *timeString =[[NSString alloc] initWithFormat:@"%d", timeTick];
self.display.text = timeString;
if(timer >= 0){
[timer invalidate];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
timeTick = 3;
}
@end
.h File
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *display;
- (IBAction)stopStartBtn:(id)sender;
@end
2 个解决方案
#1
1
Your code currently starts the timer at 0, decrements it, and then checks it it has reached 60. Obviously that won't happen.
你的代码当前启动定时器为0,减少它,然后检查它已达到60.显然这不会发生。
If you want to start at 60 and stop at 0 then you need to set timeTick
to 60
in viewDidLoad
and check for a value of 0
in myTicker
.
如果要从60开始并在0处停止,则需要在viewDidLoad中将timeTick设置为60,并在myTicker中检查值为0。
And don't forget to call [super viewDidLoad];
in your viewDidLoad
method.
并且不要忘记调用[super viewDidLoad];在viewDidLoad方法中。
You also need to fix your check to see if you've reached zero. Right now you are looking at the timer
pointer instead of timeTick
integer.
您还需要修复检查以查看是否已达到零。现在你正在查看计时器指针而不是timeTick整数。
Change:
更改:
if(timer >= 0){
[timer invalidate];
}
to:
至:
if (timeTick <= 0) {
[timer invalidate];
}
You are also never setting your timer
instance variable. You actually set a local variable of the same name.
您也永远不会设置您的计时器实例变量。实际上,您设置了一个同名的局部变量。
Change:
更改:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
to:
至:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
#2
0
@interface Ovning1 ()
{
int timeTick;
NSTimer *timer;
}
@end
@implementation Ovning1
- (IBAction)stopStartBtn:(id)sender {
[timer invalidate];
timeTick = 60;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
}
-(void)myTicker{
timeTick--;
NSString *timeString =[[NSString alloc] initWithFormat:@"%d", timeTick];
self.display.text = timeString;
if(timeTick <= 0){
[timer invalidate];
}
}
#1
1
Your code currently starts the timer at 0, decrements it, and then checks it it has reached 60. Obviously that won't happen.
你的代码当前启动定时器为0,减少它,然后检查它已达到60.显然这不会发生。
If you want to start at 60 and stop at 0 then you need to set timeTick
to 60
in viewDidLoad
and check for a value of 0
in myTicker
.
如果要从60开始并在0处停止,则需要在viewDidLoad中将timeTick设置为60,并在myTicker中检查值为0。
And don't forget to call [super viewDidLoad];
in your viewDidLoad
method.
并且不要忘记调用[super viewDidLoad];在viewDidLoad方法中。
You also need to fix your check to see if you've reached zero. Right now you are looking at the timer
pointer instead of timeTick
integer.
您还需要修复检查以查看是否已达到零。现在你正在查看计时器指针而不是timeTick整数。
Change:
更改:
if(timer >= 0){
[timer invalidate];
}
to:
至:
if (timeTick <= 0) {
[timer invalidate];
}
You are also never setting your timer
instance variable. You actually set a local variable of the same name.
您也永远不会设置您的计时器实例变量。实际上,您设置了一个同名的局部变量。
Change:
更改:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
to:
至:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
#2
0
@interface Ovning1 ()
{
int timeTick;
NSTimer *timer;
}
@end
@implementation Ovning1
- (IBAction)stopStartBtn:(id)sender {
[timer invalidate];
timeTick = 60;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTicker) userInfo:nil repeats:YES];
}
-(void)myTicker{
timeTick--;
NSString *timeString =[[NSString alloc] initWithFormat:@"%d", timeTick];
self.display.text = timeString;
if(timeTick <= 0){
[timer invalidate];
}
}