简体   繁体   English

倒数计时器将停止并重置为0

[英]Countdown timer that stops and reset on 0

Got an issue with my countdown timer who don't want to countdown from 60 and reset when 0 is reached. 我的倒数计时器遇到了一个问题,谁不想从60开始倒数​​,并在达到0时重置。 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? 目前,它只是将其标签设置为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

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 . 如果timeTick 60开始并在0处停止,则需要在viewDidLoad中将timeTick设置为60并在myTicker检查值0

And don't forget to call [super viewDidLoad]; 并且不要忘记调用[super viewDidLoad]; in your viewDidLoad method. 在您的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. 现在,您正在查看timer指针,而不是timeTick整数。

Change: 更改:

if(timer >= 0){
    [timer invalidate];
}

to: 至:

if (timeTick <= 0) {
    [timer invalidate];
}

You are also never setting your timer instance variable. 您也永远不会设置timer实例变量。 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];
@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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM