NSTimer崩溃并导致EXC_BAD_ACCESS

时间:2014-05-09 08:59:05

标签: ios objective-c nstimer appdelegate nstimeinterval

我必须检查用户是否触摸屏幕。如果用户触摸屏幕,我没有做任何事件。如果用户没有触摸屏幕并且在任何触摸时松开手指,那么在第二次触摸时,如果它达到15左右,我就会做其他事情。在这里我显示NSLog超时值。当用户在15秒后释放手指时,显示超过日志的时间。但在我执行任何按钮点击操作后,我的应用程序崩溃了。

TIMERUIApplication.h

 #define kApplicationTimeoutInMinutes 0.2

 //the notification your AppDelegate needs to watch for in order to know that it has indeed "timed out"
 #define kApplicationDidTimeoutNotification @"AppTimeOut"

 @interface TIMERUIApplication : UIApplication
 {
 NSTimer     *myidleTimer;
 }

 -(void)resetIdleTimer;

 @end

TIMERUIApplication.m:

#import "TIMERUIApplication.h"

 @implementation TIMERUIApplication

 //here we are listening for any touch. If the screen receives touch, the timer is reset
 -(void)sendEvent:(UIEvent *)event
 {
 [super sendEvent:event];

 if (!myidleTimer)
 {
 [self resetIdleTimer];
 }

 NSSet *allTouches = [event allTouches];
 if ([allTouches count] > 0)
 {
 UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
 if (phase == UITouchPhaseBegan)
 {
 [self resetIdleTimer];
 }

 }
 }
 //as labeled...reset the timer
 -(void)resetIdleTimer
 {
 if (myidleTimer)
 {
 [myidleTimer invalidate];
 myidleTimer=nil;
 }
 //convert the wait period into minutes rather than seconds
 int timeout = kApplicationTimeoutInMinutes * 60;
 myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];

 }
 //if the timer reaches the limit as defined in kApplicationTimeoutInMinutes, post this notification
 -(void)idleTimerExceeded
 {
 [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
 }


 @end

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

 self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];

 UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:self.viewController];

 self.window.rootViewController = nav1;

 [self.window makeKeyAndVisible];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];

 return YES;
 }

 -(void)applicationDidTimeout:(NSNotification *) notif
 {
 NSLog (@"time exceeded!!");


}

0 个答案:

没有答案