OGeek|极客世界-中国程序员成长平台

标题: ios - ASIHTTPREQUEST xCode - 设置委托(delegate)时崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:37
标题: ios - ASIHTTPREQUEST xCode - 设置委托(delegate)时崩溃

首先,当请求在 ViewController 中时,应用程序运行良好。我现在已经将它移动到它自己的类文件以符合 MVC 模型,但是当我发起请求时它崩溃了。我已经调试并将其范围缩小到我将委托(delegate)设置为 self 的那一行。我已经编辑了类的 .h 文件以符合 'ASIHTTPRequestDelegate',但仍然没有运气。运行暂停时我没有收到编译器错误,但是当调用该方法时它确实将我指向他的代码行:

if (delegate && [delegate respondsToSelector:didStartSelector]) {

以下代码是我用来发起请求的代码,它是从 ViewController 调用的。有什么想法吗?

-(void) authenticateLoginWithURLStringNSString *)address UsernameNSString*)username PasswordNSString*)password{
    //LoginViewController *loginVC = [LoginViewController new];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:address]];
    [request setPostValue:username forKey"u"];
    [request setPostValue:password forKey"pw"];
    [request setDelegate:self];
    [request startAsynchronous];
}

enter image description here



Best Answer-推荐答案


这就是我的想法。您会在绿色水平条的右侧看到如何显示 EXC_BAD_ACCESS。基本上,您将 self 作为委托(delegate)传递,并且该对象(self)在可以验证具有选择器之前被释放。换句话说,无论您在哪里创建该类的实例,都需要对其进行强引用,以免它被释放。

如果还不够清楚,请告诉我。

关于ios - ASIHTTPREQUEST xCode - 设置委托(delegate)时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8736275/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4