0

I have start a timer to call a method after 10 seconds. the timer start from didFinishLaunchingWithOptions. it works fine but I want to stop that timer from another UISubclass at the time of logout. How I can do this my code is

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    [NSThread sleepForTimeInterval:2.0];

    [self.window addSubview:viewController.view];
    [self setupTimer];
    [self.window makeKeyAndVisible];

    return YES;
}
-(void)setupTimer;
{
    timer = [NSTimer timerWithTimeInterval:10 
                                    target:self
                                  selector:@selector(triggerTimer:)
                                  userInfo:nil
                                   repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)triggerTimer:(NSTimer*)timer;
{
    NSLog(@"===============this method is call after every 10            second===========================");
    Getlocation *object=[[Getlocation alloc] init];
    [object updatelocation];
}
  -(void)stopTimer:(NSTimer*)timer;
 {
   [timer invalidate];
   timer=nil;
 }
4

3 回答 3

0
if([timer isValid])
     [timer invalidate];

timer = nil;
于 2012-08-22T08:34:09.580 回答
0

在注销 ButtonAction 方法中编写代码。

-(IBAction)LogOutButtonPressed:(id)sender {

[定时器无效];计时器=无;[定时器释放];

}

于 2012-08-22T09:43:57.383 回答
0

取一个 NSTimer 的对象。
写下这一行你想在哪里调用方法

timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(NEWTIMER) userInfo:nil repeats:YES];

写这段代码你想停止计时器。

[timer invalidate];
timer = nil;
[timer release];

如果您想在您的应用程序进入后台时停止计时器。您应该将此代码写入此方法。

- (void)applicationWillEnterForeground:(UIApplication *)application
{
   [timer invalidate];
   timer = nil;
   [timer release];
} 
于 2012-08-22T08:40:07.047 回答