如何让 Date 每 n 秒跳转一次?

发布于 2024-12-15 16:04:50 字数 1270 浏览 3 评论 0原文

我想让日期每“n”秒改变一天,就像一台时间机器一样。我有这段代码,但没有任何反应,任何帮助将不胜感激。 这是代码:没有问题,没有错误......没有明天的日期!

-(IBAction)jumpDate

{

NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"dd MMMM yyyy  h:mm:ss"];  //dateFormater.dateStyle   =NSDateFormatterLongStyle; //USA date style MMMM-dd-yyyy
[dateFormater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];

NSDate *todayDate = [[NSDate alloc]init];

[gregDate setText : [dateFormater stringFromDate:todayDate]];

[NSString stringWithFormat:@"dd MMMM yyyy", dayCount];

dayCount++;

if (dayCount >= 365)

{
    dayCount = 365;

    [timerDate invalidate];

}
}
//and the timer

- (void)viewDidLoad
{

NSDateFormatter *dateFormater = [[[NSDateFormatter alloc]init]autorelease];
[dateFormater setDateFormat:@"dd MMMM yyyy"];

NSTimeInterval secondsPerDay = 86400 ; // = 24 * 60 * 60

NSDate *today = [[[NSDate alloc]init]autorelease];
NSDate *tomorrow;
tomorrow = [today dateByAddingTimeInterval:(NSTimeInterval)secondsPerDay];

 [gregDate setText : [NSString stringWithFormat:@" %@ ",tomorrow]];

 dayCount = 1;
 timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(jumpDate)userInfo:nil repeats:YES];


[super viewDidLoad];
}

I want to make the date to change one day every "n" seconds, like a time machine. I have this code but nothing happens, any help will be appreciated.
this is the code: no issues, no error ... no tomorrow date!

-(IBAction)jumpDate

{

NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"dd MMMM yyyy  h:mm:ss"];  //dateFormater.dateStyle   =NSDateFormatterLongStyle; //USA date style MMMM-dd-yyyy
[dateFormater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];

NSDate *todayDate = [[NSDate alloc]init];

[gregDate setText : [dateFormater stringFromDate:todayDate]];

[NSString stringWithFormat:@"dd MMMM yyyy", dayCount];

dayCount++;

if (dayCount >= 365)

{
    dayCount = 365;

    [timerDate invalidate];

}
}
//and the timer

- (void)viewDidLoad
{

NSDateFormatter *dateFormater = [[[NSDateFormatter alloc]init]autorelease];
[dateFormater setDateFormat:@"dd MMMM yyyy"];

NSTimeInterval secondsPerDay = 86400 ; // = 24 * 60 * 60

NSDate *today = [[[NSDate alloc]init]autorelease];
NSDate *tomorrow;
tomorrow = [today dateByAddingTimeInterval:(NSTimeInterval)secondsPerDay];

 [gregDate setText : [NSString stringWithFormat:@" %@ ",tomorrow]];

 dayCount = 1;
 timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(jumpDate)userInfo:nil repeats:YES];


[super viewDidLoad];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2024-12-22 16:04:50

我有 3 个问题,并不都与你的问题直接相关。

  1. 为什么-(IBAction)jumpDate是一个IBAction,如果您从代码中调用此方法,您应该将其更改为(void),并且如果IBAction需要调用该方法,从作为该按钮操作的其他方法调用它。

  2. 此调用中 userInfo 之前的代码中是否缺少空格:
    timerDate=[NSTimerchedTimerWithTimeInterval:1.0 target:selfselector:@selector(jumpDate)userInfo:nil Repeats:YES];

  3. 您是否已验证您的 IBOutlet 是否已正确设置?我已经不再计算我忘记这一点的次数了。


[gregDate setText : [NSString stringWithFormat:@" %@ ",tomorrow]];  

什么是 gregDate?它是指向 UILabel 的 IBOutlet 吗?
您可能已经验证了这一点,但是,该调用会返回有效的字符串吗?


您不应该从计时器中调用 IBAction 方法,原因有两个,
1.我真的认为这是一个糟糕的设计,它引入了对该方法的作用的混淆,因为它也是从计时器调用的。
2. 方法签名与 NSTimer 需要的不匹配

选择器:
计时器触发时发送到目标的消息。选择器必须具有以下签名:
- (void)timerFireMethod:(NSTimer*)theTimer

此文本来自 NSTimer 类参考。

I have 3 questions, not all directly related to your question.

  1. Why -(IBAction)jumpDate is an IBAction, if you are calling this method from your code you should changed it to (void) and if an IBAction need to call that, call it from an other method that would be the action for that button.

  2. Is there a missing space in your code before userInfo in this call :
    timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(jumpDate)userInfo:nil repeats:YES];

  3. Did you verified that your IBOutlet are all set properly? I've stop counting the times I've forgot that.


[gregDate setText : [NSString stringWithFormat:@" %@ ",tomorrow]];  

What is gregDate? is it an IBOutlet pointing to a UILabel?
And you've probably verified this, but, that call retunr a valid string?


You should not call your IBAction method from your timer, for 2 reasons,
1. I really think it's bad design, it's introducing confusion on the role of this method since it's also call from a timer.
2. The method signature doesn't match the one NSTimer needs

aSelector:
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer

This text come from the NSTimer class reference.

小ぇ时光︴ 2024-12-22 16:04:50

将此行: 替换

NSDate *todayDate = [[NSDate alloc]init];

为此行:

NSDate *todayDate = [[NSDate date] dateByAddingTimeInterval:dayCount * 86400];

Replace this line:

NSDate *todayDate = [[NSDate alloc]init];

with this line:

NSDate *todayDate = [[NSDate date] dateByAddingTimeInterval:dayCount * 86400];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文