在我的iPhone计时器应用程序中
其中计时器应该在后台运行。
所以,我已经将通知设置为set委托--它工作得很好.使用该方法,我将从视图控制器调用方法,使计时器活动。
看看一些代码..。
App委托
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
NSLog(@"Time Remaining %d",(self.viewController.totalSeconds-self.viewController.totalCount));
[self.viewController selectandnotify:(self.viewController.totalSeconds-self.viewController.totalCount)];
[self.viewController stopTimer];
[self.viewController startTimerAction];
}在这里,我调用方法startTimerAction方法,在我看来,controller...take是看这个.
-(void)startTimerAction
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES];
}那就是NSTimer
每次都在这里
-ShowActivity方法将在每个second...Which在我的视图控制器下面之后调用.
-(void)ShowActicity
{
NSLog(@"Total Counts %d",totalCount);
if (totalCount == totalSeconds) {
if ([timer_main isValid]) {
[timer_main invalidate];
isTimeOver = YES;
[self generateLog];
}
} else {
totalCount++;
seconds =seconds + 1;
if(seconds > 59)
{
minutes = minutes + 1;
seconds= 0;
}}
如何每次从视图控制器调用此方法.
如何每次从appdelegate...调用showActivity方法?
- Should I use delegate for that
- Should I create showActivity and timer in my Appdelegate..
我认为如果我做代表是一个好的选择?
任何其他way....please都有一些建议
发布于 2011-12-23 08:07:06
通常将此代码用于运行.In的后台,后台计时器不工作
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[self startTimerAction];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3
https://stackoverflow.com/questions/8613487
复制相似问题