我必须检测用户是否触摸过iPhone屏幕。因此,我在我的项目中创建了一个名为"CustomApplication“的类(子类化UIApplication),然后,我修改了我的main.m,如下所示:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, @"CustomApplication",nil);
[pool release];
return retVal;CustomApplication.m类包含如下方法:
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
[MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // showing an alert here
}方法showAlertWithTitle如下所示:
+ (void) showAlertWithTitle:(NSString *)aTitle message:(NSString *)aMessage
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show]; // Line causing problem in iOS 5 - base sdk 5.0
[alertView release];}
在sendEvent 4.2中一切正常,但在iOS 5.0中,应用程序在触摸屏幕时崩溃(当调用iOS :event方法时)。当我调试代码时,我发现问题出在alertView显示;行中。在iOS 5中,当执行这行代码(alertView show;)时,它再次调用CustomApplication的sendEvent方法,此方法调用showAlertWithTitle: MyUtility的方法,该方法再次调用sendEvent方法,因此,代码进入无限循环。我不知道解决方案。如果有人遇到过这种奇怪的事情,那么请告诉我,我应该写些什么,以便在显示警报时不会调用sendEvent方法?
发布于 2012-03-01 21:47:10
这可能看起来不是最好的方法,但它可能会奏效。您是否尝试过在代码中简单地包含一个静态布尔变量,以便函数不会再次被调用?
在上面的某个地方
BOOL stopHere = NO;然后是sendEvent
-(void)sendEvent:(UIEvent *)event{
[super sendEvent:event];
if (!skipHere){
skipHere = YES;
[MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // show
skipHere=NO;
}
}https://stackoverflow.com/questions/9380295
复制相似问题