gpt4 book ai didi

ios - 导致错误的 UIAlertView 按钮操作

转载 作者:行者123 更新时间:2023-11-29 13:07:51 25 4
gpt4 key购买 nike

我正在向用户显示一个 UIAlertView,上面有一个“确定”按钮,当用户按下按钮时,我希望我的委托(delegate)方法执行一个操作。然而,目前当用户按下“确定”按钮时,应用程序崩溃并出现此错误:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xb)

这是我的代码,警报 View 通过按钮等显示正常,但是一旦按下按钮,这就是我收到的错误。这几乎就像一个断点,但如果我点击前进按钮,什么也不会发生。

//.h
@interface ErrorHandling : NSObject <UIAlertViewDelegate> {


//.m
#define myAlertViewsTag 0

- (void)recivedErrorCode:(int)errorCode MethodName:(NSString *)methodName {
switch (errorCode) {
case 1: {
NSLog(@"ERROR = 1");
message = [[UIAlertView alloc] initWithTitle:@"Error 1:"
message:@"The supplied registration code does exist."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[SVProgressHUD dismiss];
[message show];
}
break;
case 2: {
NSLog(@"ERROR = 2");
message = [[UIAlertView alloc] initWithTitle:@"Error 2:"
message:@"Your registration is no longer valid."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
message.tag = myAlertViewsTag;
[SVProgressHUD dismiss];
[message show];
}
break;


default:
break;
}
}


-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (message.tag == myAlertViewsTag) {
if (buttonIndex == 0) {
// Do something when ok pressed
NSLog(@"DONE 1");
} else {
// Do something for ok
NSLog(@"DONE 2");
}
} else {
// Do something with responses from other alertViews
NSLog(@"DONE 3");
}
}

更新:这就是我从我的连接类中调用上面代码所在的类的方式。

 // Do whatever you need to with the error number
ErrorHandling *errorHandling = [[ErrorHandling alloc] init];
[errorHandling recivedErrorCode:errorRecivedFromServerInt MethodName:methodName];

最佳答案

问题在于您的 ErrorHandling 实例的生命周期。您创建一个实例,然后调用 recivedErrorCode: 方法。在那之后没有其他对您的 errorHandling 实例的强引用,因此它被释放。

与此同时,警报 View 已将实例作为其委托(delegate)。当您点击警报 View 上的按钮时,警报 View 会尝试联系其代理。但委托(delegate)已被释放,现在指向导致崩溃的垃圾内存。

解决方案是对 ErrorHandling 实例保持更持久的强引用。至少在关闭警报 View 之前。

顺便说一句 - 您的方法名称有错字 - 它应该是 receivedErrorCode:

关于ios - 导致错误的 UIAlertView 按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177597/

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