无法获取ASIHTTPRequest回调委托来触发

时间:2011-07-14 11:42:35

标签: php iphone xcode asihttprequest

为什么我的回调委托不会触发:

#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文件,这些是我唯一的两个文件..我做错了什么?

2 个答案:

答案 0 :(得分:3)

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

+(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
    }

}

编辑2 将委托参数传递给函数。

+(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];    
}

答案 1 :(得分:0)

关于你的代码我不确定的部分是将请求触发器放在另一个类的类方法中。你知道[dbConnector getQuestions:@"2"];实际上正在触发预期的方法吗?