我在我的.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