调用NSTimer方法
问题描述:
我是iPhone
定时器的新手,需要一些支持。调用NSTimer方法
我有一个方法如下,需要时间和更新UILabel
在我的userinterface
。我也有一个NSTimer
,每秒调用一次该方法(我只显示小时和分钟)。这工作得很好,除了第一秒的应用程序是活的(据我所知在timer
调用方法之前需要一秒钟)。
我想从viewDidLoad
打电话给我的方法,但我该如何提供正确的参数?或者有更好的方法来做到这一点?
// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
-(void)updateTime: (NSTimer *) timer {
currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
[dateFormatter release];
timeLabel.text = currentTimeString;
}
欣赏任何会指向正确方向的东西。
答
在你的方法中,你不使用计时器变量吗?那么,为什么只需拨打[self updateTime:nil]
。这应该工作
答
还有一个选项...你可以这样做:
[NSTimer scheduledTimerWithTimeInterval:0.01f
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats: YES];
+0
谢谢贺拉斯。 我想我会去vodkhangs解决方案。 – Structurer 2010-06-22 10:07:56
是的,你是对的。这样一个简单而整洁的解决方案。经过测试,工作正常。 谢谢! 似乎也是一个解决方案与“火”,你可以触发计时器事件,但我还没有时间来测试它呢。 – Structurer 2010-06-22 10:09:11