-2

I'm using Xcode with Objective C language. I wanted to know if there is any method to show a timer or a counter which shows as to how long the application is being running. It simple increments the counter to seconds, minutes however long the app is running. Any help would be appreciated.

Thank You

  • Check this out as it have a sample project demonstrating your need http://www.techrepublic.com/blog/software-engineer/create-your-own-apple-iphone-clock-app/ – zaheer Dec 24 '14 at 04:35
  • Simply create a repeating NSTimer and update the label every second with the elapsed time on the main thread. – Lyndsey Scott Dec 24 '14 at 04:37
  • So how would I display time, @zaheer: that shows the current time, how will I show the elapsed time? – Aarush Gupta Dec 24 '14 at 04:43
  • are you developing for ios or osx... you could do something like in the app did finish launching save time as variable then run a loop that does math stuff – nsij22 Dec 24 '14 at 04:52
  • Developing for ios(iphone). Also, I checked up this thread.. http://stackoverflow.com/questions/11018854/show-time-like-a-digital-clock as you can see, I am not able to connect the timer IBOutlet with the main storyboard i don't know why. – Aarush Gupta Dec 24 '14 at 04:59

1 Answers1

0

Create one UILable in your RootViewController or UIWindow and then update it frequently with following way.

create one Variable in .h file.

@property (nonatomic) int seconds;

put following code in ViewDidLoad method of your RootViewController

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

Create following Function to update 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]];

}
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
  • So where do I put the 2 codes that you've mentioned. I am using Xcode 6. So where to put them? – Aarush Gupta Dec 24 '14 at 05:10
  • you can put in your `RootViewController` or you can put in `AppDelegate` also. – Abhishek Sharma Dec 24 '14 at 05:17
  • How do I solve the error: Property 'seconds' not found on the object of ViewController ; did you mean to access instant variable seconds. I have put a UILabel: IBOutlet UILabel *seconds; And I have added the code under ViewController.m: - (void) updateLabel { self.oseconds = 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]]; } Now? – Aarush Gupta Dec 24 '14 at 05:23
  • And also property 'lbldemo' not found on the object type 'ViewController'.. Sorry newly coding here.. can I have your email to talk this through quickly? If it's possible.. thanks for the help.. – Aarush Gupta Dec 24 '14 at 05:24
  • declare two `variable` in `.h` file like below.`@property (nonatomic, weak) IBOutlet UILabel * lblDemo; @property (nonatomic) int seconds;` – Abhishek Sharma Dec 24 '14 at 05:31
  • And where does lbl.demo and seconds have to be on "View Controller" as it gives me an error again and again saying: Property 'seconds' not found on object type "View Controller" – Aarush Gupta Dec 24 '14 at 05:37
  • you have to `declare` that variable in same `class` `.h` file. suppose you are created `Viewcontroller class` then in `ViewController.m` have all `code` and in `ViewController.h` have property declaration. – Abhishek Sharma Dec 24 '14 at 05:45
  • could you tell me your email? I can send you a copy of the code, just have one quick question, can't explain like this. or contact me: aarushg94@gmail.com Would really be helpful. Thanks.. let me know – Aarush Gupta Dec 24 '14 at 05:56
  • I understand the concept of where to put what. but it says that it is not present on the ViewController. What all labels/buttons/text field should I insert in the main.storyboard? – Aarush Gupta Dec 24 '14 at 05:57
  • Also, all of it is under same class still the error's showing: ViewController.m:309:25: Property 'seconds' not found on object of type 'ViewController *' and getting one last error: ViewController.m:309:35: Arithmetic on pointer to interface 'UILabel', which is not a constant size for this architecture and platform – Aarush Gupta Dec 24 '14 at 06:04