gpt4 book ai didi

iphone - -(无效)sendEvent :(UIEvent *)event method iPhone 5. 0

转载 作者:行者123 更新时间:2023-11-29 13:40:19 24 4
gpt4 key购买 nike

我必须检测用户是否触摸了 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];

在 iOS 4.2 中一切正常,但在 iOS 5.0 上应用程序在触摸屏幕时崩溃(当调用 sendEvent:event 方法时)。我调试代码的时候发现问题出在[alertView show];线。在 iOS 5 中,发生的事情是当这一行 ([alertView show];) 被执行时,它再次调用 CustomApplication 的 sendEvent 方法,该方法调用 showAlertWithTitle: MyUtility 的方法,后者又再次调用 sendEvent 方法,因此,代码正在进入无限循环。我不知道解决方案。如果有人遇到过这种奇怪的事情,那么请告诉我应该写什么,以便在显示警报时不会调用 sendEvent 方法?

最佳答案

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

在某处

BOOL stopHere = NO;

然后是发送事件

-(void)sendEvent:(UIEvent *)event{
[super sendEvent:event];
if (!skipHere){
skipHere = YES;
[MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // show
skipHere=NO;
}
}

关于iphone - -(无效)sendEvent :(UIEvent *)event method iPhone 5. 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9380295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com