如何让 Date 每 n 秒跳转一次?
我想让日期每“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有 3 个问题,并不都与你的问题直接相关。
为什么
-(IBAction)jumpDate
是一个IBAction,如果您从代码中调用此方法,您应该将其更改为(void)
,并且如果IBAction需要调用该方法,从作为该按钮操作的其他方法调用它。此调用中
userInfo
之前的代码中是否缺少空格:timerDate=[NSTimerchedTimerWithTimeInterval:1.0 target:selfselector:@selector(jumpDate)userInfo:nil Repeats:YES];
您是否已验证您的 IBOutlet 是否已正确设置?我已经不再计算我忘记这一点的次数了。
什么是 gregDate?它是指向 UILabel 的 IBOutlet 吗?
您可能已经验证了这一点,但是,该调用会返回有效的字符串吗?
您不应该从计时器中调用
IBAction
方法,原因有两个,1.我真的认为这是一个糟糕的设计,它引入了对该方法的作用的混淆,因为它也是从计时器调用的。
2. 方法签名与 NSTimer 需要的不匹配
此文本来自 NSTimer 类参考。
I have 3 questions, not all directly related to your question.
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.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];
Did you verified that your IBOutlet are all set properly? I've stop counting the times I've forgot that.
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
This text come from the NSTimer class reference.
将此行: 替换
为此行:
Replace this line:
with this line: