2013-05-10 39 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; 

因此,無論何時發生的情況下,定時器不會停止,然後輸入此代碼定時器將永久停止。