ios - 为 iOS 应用程序制作计时器

标签 ios chronometer

我刚刚接触应用程序编程,并且刚刚开始进入 iOS 世界。

我正在尝试自学,只是按照我在网上找到的类(class)(CS193P - 斯坦福大学)进行学习。

我认为这个问题很简单。

我想实现一个计时器(对于一个总是迟到的 friend ,想计时我等他的时间哈哈),但我不知道如何操作。 “清爽标签”?

有什么建议吗?提前致谢。

顺便说一句,如果您认为我感兴趣的任何类(class),我都会非常感激。

最佳答案

对于这种“令人耳目一新的东西”,你应该使用 NSTimer:

//initialize this in your viewDidload
[NSTimer scheduledTimerWithTimeInterval:1.0
    target:self
    selector:@selector(refreshLabel)
    userInfo:nil
    repeats:Yes];

然后,在 Controller 中实现在上面的选择器参数中传递的方法:

-(void)refreshLabel
{
   //logic here: incrementing some counter and setting in your label
}

如果您想要更短的时间,只需在 scheduledTimerWithTimeInterval 参数中更改即可。

当然,这就是您将标签设置为每秒刷新一次的方式(假设您有一个 IBOutlet 为 1)。您仍然需要创建 chrono 的逻辑(增加一些实例变量,创建一些逻辑来分割分钟/秒,或使用 NSDate 方法)。如果您仍需要其他方面的帮助,请告诉我们。

Josh Caswell 评论中的链接会对您有很大帮助。

如果您了解有关 NSTimer 的更多信息,请阅读 Apple 的文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html#//apple_ref/doc/uid/20000807-CJBJCBDE

关于ios - 为 iOS 应用程序制作计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038084/

相关文章:

ios - 在 swift xcode 中向屏幕添加边框

java - 如何调用另一个类的方法?

android - 天文台字体在android中没有改变

java - android - 计时器不从零开始

android - 计时码表不会覆盖基值

ios - 无效更新: invalid number of rows in section 0 when trying to expand and collapse UITableView sections

ios - React-Native:iOS 上的 Axios 问题

ios - DJI SDK调用DJIMissionManager.prepare方法后报错 "Application is not registered"Code=-1001

ios - 访问 NSMutableArray 时出现问题

android - 在 Android 中为计时间隔创建一个正在运行的秒表