无法获取 ASIHTTPRequest 回调委托来触发 [英] Cannot Get ASIHTTPRequest callback delegate to trigger

查看:33
本文介绍了无法获取 ASIHTTPRequest 回调委托来触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的回调委托没有触发:

Why does my callback delegate not trigger:

#import "dbConnector.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h";

@implementation dbConnector

//method to 
+(void)getQuestions:(NSString*)sectionId{
    NSString* url = @"http://dev.speechlink.co.uk/David/tester.php";
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:sectionId forKey:@"section"]; 
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];    
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    //NSString *response = [request responseString];
    NSLog(@"hello"); //never prints
}

@end


#import "ASITesterViewController.h"
#import "dbConnector.h";

@implementation ASITesterViewController
@synthesize questions;

-(void)viewDidLoad{
    //code to initialise view
    [dbConnector getQuestions:@"2"];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

减去相应的头文件和相应的 ASIHTTPRequest 文件,这是我仅有的两个文件.我做错了什么?

Minus the corresponding header files and the appropriate ASIHTTPRequest Files, these are the only two files I have.. What am I doing wrong?

推荐答案

它不会被调用 b/c 您创建了一个类方法,因此它无法访问选择器方法.

It will not get called b/c you created a class method so it does not have an access to the selector method.

+(void)getQuestions:(NSString*)sectionId

使用同步调用:

+(void)getQuestions:(NSString*)sectionId{
    NSString* url = @"http://dev.speechlink.co.uk/David/tester.php";
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:sectionId forKey:@"section"];   

    [request startSynchronous];

    NSError *error = [request error];
    if (!error) {
        //Do what you want with the response
    }else{
        //Error
    }

}

EDIT 2 将委托参数传递给函数.

EDIT 2 Pass a delegate parameter to the function.

+(void)getQuestions:(NSString*)sectionId respondToDelegate:(id)delegate{
    NSString* url = @"http://dev.speechlink.co.uk/David/tester.php";
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:sectionId forKey:@"section"]; 
    [request setDelegate:delegate];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];    
}

这篇关于无法获取 ASIHTTPRequest 回调委托来触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆