2013-05-10 67 views
7

我在我的.h文件中下面的代码:的NSTimer不停止在已失效

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <AVFoundation/AVFoundation.h> 
#import <MapKit/MapKit.h> 

@interface LandingController : UIViewController<CLLocationManagerDelegate> { 
    CLLocationManager *LocationManager; 
    AVAudioPlayer *audioPlayer; 

} 

@property (nonatomic, retain) NSTimer *messageTimer; 

- (IBAction)LoginButton:(id)sender; 

@end 

我在我的.m文件下面的代码:

@interface LandingController() 

@end 

@implementation LandingController 
@synthesize messageTimer; 

- (void)checkForMessages 
{ 

    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"BINGO:" 
          message:@"Bingo This Works" 
          delegate:nil 
          cancelButtonTitle:@"Okay" 
          otherButtonTitles:nil]; 

    [alert show]; 

} 

- (IBAction)LoginButton:(id)sender { 

    if ([UserType isEqualToString:@"Owner"]) { 

     if (messageTimer){ 
     [self.messageTimer invalidate]; 
     self.messageTimer = nil; 
     } 

    } else { 

     if (!messageTimer){ 

      self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 
                  target:self 
            selector:@selector(checkForMessages) 
                  userInfo:nil 
                  repeats:YES]; 


     } 
    } 

} 

@end 

但我的计时器不当我致电无效时,我想停下来。

LoginButton只按两次,一次strResult = =“Guard”,然后应用程序将其更改为“Owner”,并且用户再次按下登录按钮,所以我不认为我设置多个定时器。

按下登录按钮并启动计时器后,我继续进入另一个视图,然后再继续按下登录按钮,这是我希望计时器停止的时间。我是否需要做特别的事情来获取messageTimer,因为我切换了一段时间然后又回来了?

任何想法?

谢谢!

+0

你在哪写了scheduledTimerWithTimeInterval代码?检查一下,即使你使它无效,它也必须重新初始化。 – 2013-05-10 05:30:19

+1

我把它写在一个if循环中,只在应用程序中发生一次,所以我不认为它正在被重新初始化。 – NCoder 2013-05-10 05:32:12

+0

我已经添加了更多代码 - 请参阅上面的 – NCoder 2013-05-10 13:27:30

回答

28

您需要在创建定时器的同一线程上调用[self.messageTimer invalidate]。只要确保定时器在主线程上被创建并失效。

dispatch_async(dispatch_get_main_queue(), ^{ 
    if ([UserType isEqualToString:@"Owner"]) { 
     [self.messageTimer invalidate]; 
     self.messageTimer = nil; 
    } else { 
     self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 
                  target:self 
                  selector:@selector(checkForMessages) 
                  userInfo:nil 
                  repeats:YES]; 
    } 
}); 
+0

如果我打电话给ELSE内部的人,而另一个内部的他们则不在同一主线上? – NCoder 2013-05-10 13:00:48

+0

当用户按下按钮并启动时(IBAction)LoginButton:(id)sender {// if else here here} – NCoder 2013-05-10 13:01:44

+0

我添加了更多的代码 - 参见上面的代码 – NCoder 2013-05-10 13:23:58

13

NSTimer由NSRunLoop保留,所以我看到您的问题发生的唯一方法是如果您实际上创建多个计时器并且仅使您所引用的内容失效。

例子:

if(!timer) 
     timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(processTimer) userInfo:nil repeats:YES]; 
+0

我把它写在一个if循环中,只在应用程序中发生一次,所以我不认为它正在被重新初始化。任何其他想法? – NCoder 2013-05-10 05:33:24

+0

让我们看看代码 – 2013-05-10 05:34:51

+0

我已经用IF循环更新了代码。页面加载时的第一个状态不是OWNER,然后切换到OWNER,它应该触发停止计时器。我知道这是正确的if语句,因为我把这两个部分的警报进行验证。唯一的是计时器不会停止。 – NCoder 2013-05-10 05:40:36

3

你有没有试图把重复的无

self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 
                  target:self 
                  selector:@selector(checkForMessages) 
                  userInfo:nil 
                  repeats:NO]; 
+0

但是,如果我希望它每隔10秒运行一次计时器,我是否需要重复设置为YES? – NCoder 2013-05-10 05:42:27

+0

它看起来像是我将它设置为NO,它只运行一次计时器,但我绝对需要它连续运行,直到我告诉它停止。 – NCoder 2013-05-10 05:45:05

+0

ok然后从你调用这个if([UserType isEqualToString:@“Owner”]){}?这是在相同的类实例或其他? – wesley 2013-05-10 05:50:03

1

如何把一个NSLogcheckForMessages方法是什么?检查是否真的只有一个定时器会更容易。

(我宁愿把这个评论,但我没有那么多的声誉....)

+0

我已经在日志和警报中添加了以验证只有一个计时器正在运行,并且我确信只有一个正在运行。定时器启动警报只发生一次,定时器停止警报只发生一次,但定时器继续运行。 – NCoder 2013-05-10 12:54:07

+0

我已经添加了更多的代码 - 见上面 – NCoder 2013-05-10 13:18:05

2

如果在最后的代码(从IF)与用户类型调用了两次! =所有者,您创建一个新的计时器而不会使旧计时器无效。现在两个计时器正在运行。如果代码执行第三次,您将添加第三个计时器等。使用UserType ==执行代码所有者只会使最后一个计时器无效,即使它被重复调用,也不会使旧计时器失效。

你有一个计时器泄漏。 ;-)

+0

我希望它是一个泄漏!我已经在日志和警报中添加了以验证只有一个计时器正在运行,并且我确信只有一个正在运行。定时器启动警报只发生一次,定时器停止警报只发生一次,但定时器继续运行。任何其他想法? – NCoder 2013-05-10 12:54:46

+0

我已经添加了更多代码 - 请参阅上面 – NCoder 2013-05-10 13:17:21

+0

我不明白,您想对您的评论发表什么意见。然而,一个定时器继续运行,即使'NSTimer'的实例也被释放。你必须发送'-invalidate'。 – 2016-11-10 14:24:19

0

我有停止或禁用定时器的方法之前,将此确保您尝试了所有的情况下,如上面提到的,所以你也可以理解为什么这种方法在最后一次使用。

// Instead of self use NSTimer class, it will not provide you any crash and in selector placed any empty function and setRepeats to NO 

self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:100.0 
         target:NSTimer selector:@selector(emptyFunctionCalling) 
                    userInfo:nil 
                    repeats:NO]; 
[self.messageTimer invalidate]; 
self.messageTimer = nil; 

因此,无论何时发生的情况下,定时器不会停止,然后输入此代码定时器将永久停止。