开发者

how to integrate countdown and alarm in xcode iphone? please provide source code [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_JAVA技巧 Closed 11 years ago.

how to integrate countdown and alarm in xcode iphone? please provide source code..... i have tried a lot...but didn't find any specific solution...please help me...


A countdown would not be very hard to integrate, just use an NSTimer:

NSTimer *countdown = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES]; 

Simply countdown the timer for as many seconds as you want:

- (void)timerTicked:(NSTimer *)countdown{
    seconds--; // Some pre-declared variable

    // UI Updates Here if you want

    if (seconds <= 0)
        [countdown invalidate]; 
}

An alarm would be a little more complicated, but still not too hard. There's a few ways you could do it depending how you are setting the alarm. If it's soon just use a timer:

NSTimer *alarm = [NSTimer timerWithTimeInterval:SECONDS target:self selector:@selector(alarmDoneMethod) userInfo:nil repeats:NO];

However I'm assuming for an alarm you want it to be able to be set hours or days in the future. In this case use an NSDate.

NSDateComponents *alarmComponents = [[NSDateComponents alloc] init]; 
[alarmComponents setMinute:userInputMinute];
[alarmComponents setHour:userInputHour]; 
[alarmComponents setDay:userInputDay];
[alarmComponents setMonth:userInputMonth];
[alarmComponents setYear:userInputYear];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *alarm = [gregorian dateFromComponents:alarmComponents];
[alarmComponents release]; 

Now just set a timer (I'm sure there is a more efficient way of doing this but I can't think of it off the top of my head at the moment, it's still early in the day) to check if your alarm has been reached:

NSTimer *checkAlarm = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(checkAlarm:) userInfo:nil repeats:YES]; // Checks every minute

Then inside the checkAlarm: method just see if the alarm has been reached:

-(void)checkAlarm:(NSTimer *)t{
    if ([[NSDate date] earlierDate:alarm] == alarm){
        // Alarm reached
        [t invalidate]; 
    }
}

Hope this helps.

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜