首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >- (void)sendEvent:(UIEvent *)事件方法iPhone 5.0

- (void)sendEvent:(UIEvent *)事件方法iPhone 5.0
EN

Stack Overflow用户
提问于 2012-02-21 23:33:36
回答 1查看 2K关注 0票数 1

我必须检测用户是否触摸过iPhone屏幕。因此,我在我的项目中创建了一个名为"CustomApplication“的类(子类化UIApplication),然后,我修改了我的main.m,如下所示:

代码语言:javascript
复制
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, @"CustomApplication",nil);
[pool release];
return retVal;

CustomApplication.m类包含如下方法:

代码语言:javascript
复制
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
[MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // showing an alert here
}

方法showAlertWithTitle如下所示:

代码语言:javascript
复制
+ (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方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-01 21:47:10

这可能看起来不是最好的方法,但它可能会奏效。您是否尝试过在代码中简单地包含一个静态布尔变量,以便函数不会再次被调用?

在上面的某个地方

代码语言:javascript
复制
BOOL stopHere = NO;

然后是sendEvent

代码语言:javascript
复制
-(void)sendEvent:(UIEvent *)event{
    [super sendEvent:event];
    if (!skipHere){
        skipHere = YES;
        [MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // show        
    skipHere=NO;
    }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9380295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档