可能是下面的代碼將幫助您:
第1步:申報以下實例變量
@interface yourclass:NSObject的 {
NSTimer* myTimer;
NSDate *initialDate;
}
第2步:在您的類中創建myTimer您想:
myTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(callByTimer) userInfo:nil repeats:YES];
第3步:實現callByTimer方法:
- (void)callByTimer{
// get initial date at very first call by myTimer
if (!initialDate)
{
initialDate = [NSDate date];
}
// get current date
NSDate *currentDate = [NSDate date];
// get seconds between currentdate and initial date
NSTimeInterval secondsBetween = [currentDate timeIntervalSinceDate:initialDate];
// convert seconds into minutes
NSInteger minutes = secondsBetween/60;
// check if minutes is greater than or equal to 3 then invalidate myTimer and assign nil to initialDate variable
if (minutes>=3)
{
[myTimer invalidate];
initialDate = nil;
}}
int值的totalseconds? – Thanks
這真的很棒非常感謝 – Thanks
@Thanks隨時歡迎:) –