iOS 开发 计时器秒数准确 oc代码
以下是一个简单的iOS开发中计时器秒数准确的Objective-C代码示例:
// 在.h文件中声明属性
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger seconds;
// 在.m文件中实现逻辑
- (void)viewDidLoad {
[super viewDidLoad];
self.seconds = 0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
- (void)updateTimer {
self.seconds++;
NSLog(@"计时器秒数:%ld", (long)self.seconds);
}
- (void)dealloc {
[self.timer invalidate];
}
在上述代码中,我们创建了一个NSTimer对象,并在每秒钟触发一次updateTimer方法。在updateTimer方法中,我们将计时器秒数increment了1,并在控制台打印出来。在视图控制器销毁时,我们需要调用[self.timer invalidate]来停止计时器并释放内存
原文地址: http://www.cveoy.top/t/topic/iQwl 著作权归作者所有。请勿转载和采集!