【问题标题】:Show timer in an app? [closed]在应用程序中显示计时器? [关闭]
【发布时间】:2014-12-24 04:29:43
【问题描述】:

我正在使用 Xcode 和 Objective C 语言。我想知道是否有任何方法可以显示计时器或计数器,以显示应用程序正在运行多长时间。它简单地将计数器增加到秒,分钟,无论应用程序运行多长时间。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 检查一下,因为它有一个示例项目来证明您的需求techrepublic.com/blog/software-engineer/…
  • 只需创建一个重复的 NSTimer 并使用主线程上经过的时间每秒更新一次标签。
  • 那么我将如何显示时间,@zaheer:显示当前时间,我将如何显示经过的时间?
  • 你是在为 ios 还是 osx 开发......你可以在应用程序中执行类似的操作,完成启动保存时间作为变量,然后运行一个循环来处理数学问题
  • 为 ios(iphone) 开发。另外,我检查了这个线程.. stackoverflow.com/questions/11018854/… 如你所见,我无法将计时器 IBOutlet 与主故事板连接,我不知道为什么。

标签: ios objective-c iphone xcode development-environment


【解决方案1】:

在你的RootViewControllerUIWindow中创建一个UILable,然后通过以下方式经常更新。

.h 文件中创建one Variable

@property (nonatomic) int seconds;

RootViewControllerViewDidLoad 方法中加入code

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
    [timer fire];

创建关注Function以更新UILabel

- (void) updateLabel {

    self.seconds = self.seconds + 1;
    NSLog(@"You can show time in minutes, hours and day also after doing some calculation as per your need");
    [self.lblDemo setText:[NSString stringWithFormat:@"%d seconds",self.seconds]];

}

【讨论】:

  • 那么我把你提到的两个代码放在哪里。我正在使用 Xcode 6。那么将它们放在哪里?
  • 你可以输入你的RootViewController或者你也可以输入AppDelegate
  • 如何解决错误:在 ViewController 的对象上找不到属性“秒”;你的意思是访问即时变量秒。我放了一个 UILabel: IBOutlet UILabel *seconds;我在 ViewController.m 下添加了代码: - (void) updateLabel { self.oseconds = self.seconds + 1; NSLog(@"您可以根据需要进行一些计算后以分钟、小时和天为单位显示时间"); [self.lblDemo setText:[NSString stringWithFormat:@"%d seconds",self.seconds]]; } 现在?
  • 而且在对象类型“ViewController”上找不到属性“lbldemo”。抱歉,这里是新编码。。我可以让你的电子邮件快速解决这个问题吗?如果可能的话..感谢您的帮助..
  • .h 文件中声明两个variable,如下所示。@property (nonatomic, weak) IBOutlet UILabel * lblDemo; @property (nonatomic) int seconds;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
  • 2015-01-27
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多