我在我的.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,因爲我切換了一段時間然後又回來了?
任何想法?
謝謝!
你在哪寫了scheduledTimerWithTimeInterval代碼?檢查一下,即使你使它無效,它也必須重新初始化。 – 2013-05-10 05:30:19
我把它寫在一個if循環中,只在應用程序中發生一次,所以我不認爲它正在被重新初始化。 – NCoder 2013-05-10 05:32:12
我已經添加了更多代碼 - 請參閱上面的 – NCoder 2013-05-10 13:27:30