• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?


  • Subject: Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
  • From: Ashley Clark <email@hidden>
  • Date: Wed, 10 Dec 2008 18:36:07 -0600

If you save off a starting time object you could use NSDateComponents.


NSDate *startDate = /* set at beginning */
NSCalendar *cal = [NSCalendar currentCalendar];
NSCalendarUnit units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;


NSDateComponents *comps = [cal components:units fromDate:startDate toDate:[NSDate date] options:0];

The resulting comps object will respond to -hour, -minute and -second messages.


Ashley

On Dec 10, 2008, at 6:03 PM, Chunk 1978 wrote:

i believe i painted myself into a corner here... i have a
NSPopUpButton with 3 items.  1 Hour, 2 Hours, 3 Hours.  each item has
respected tag numbers 1, 2 and 3.  i'm attempting to print out time
remaining but i can only get as far as displaying seconds remaining
with this:  NSLog(@"%.2d Seconds Remaining", (hoursSelected -
second));  but i would like for the log to output @"%.2d Hours, %.2d
Minutes and - Seconds Remaining";  i can't wrap my head around it,
and i fear that my trying to be as if/else statementless as possible
by using the tag numbers of the PopUp Menu is causing me problems.

-=-=-=-=-

-(int)timeMenuSelection
	{
	return [[menu selectedItem] tag];
	}

- (IBAction)startTimer:(id)sender
	{
	startTime = [NSDate timeIntervalSinceReferenceDate];

	[killTimer invalidate];
	[killTimer release];
	killTimer = nil;

	killTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(updateTime:) userInfo:nil repeats:YES] retain];
	}

- (void)updateTime:(NSTimer *)theTimer
	{
	NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
	NSTimeInterval interval = now - startTime;
	int second = (int)interval;

	//Tag #1 x 3600 Seconds = 3600 Seconds = 2 Hours.
	//Tag #2 x 3600 Seconds = 7200 Seconds = 2 Hours.
	//Tag #3 x 3600 Seconds = 10800 Seconds = 3 Hours.

	int hoursSelected = ([self timeMenuSelection] * 3600);

	if (second <= hoursSelected)
		{
		NSLog(@"%.2d Seconds Remaining", (hoursSelected - second));
		}
		else
		{
		NSLog(@"TIME'S UP!");
		[killTimer invalidate];
		[killTimer release];
		killTimer = nil;
		}
	}

-=-=-=-=-
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
      • From: "Chunk 1978" <email@hidden>
References: 
 >Countdown With NSTimer - Hours, Minutes, Seconds Remaining? (From: "Chunk 1978" <email@hidden>)

  • Prev by Date: Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
  • Next by Date: Problem setting a Core Data Document Icon
  • Previous by thread: Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
  • Next by thread: Re: Countdown With NSTimer - Hours, Minutes, Seconds Remaining?
  • Index(es):
    • Date
    • Thread