【问题标题】:Cannot Get ASIHTTPRequest callback delegate to trigger无法获取 ASIHTTPRequest 回调委托以触发
【发布时间】:2011-07-14 11:42:35
【问题描述】:

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

#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 文件,这是我仅有的两个文件。我做错了什么?

【问题讨论】:

    标签: php iphone xcode asihttprequest


    【解决方案1】:

    它不会被调用 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
        }
    
    }
    

    EDIT 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];    
    }
    

    【讨论】:

    • 对不起,我该如何解决这个问题?我想使用一个类方法,因为 dbConnector 类只是一个库类
    • 我不想使用同步呼叫...我希望应用程序在数据提交时能够正常工作。
    • 为此,您必须在单独的线程上启动调用。这样您的应用程序就可以在您建立网络时运行。
    • Google 是你的朋友,你可以在堆栈溢出时找到它
    • 查看我的第二个编辑,您可以将委托作为参数传递。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2013-02-21
      • 1970-01-01
      相关资源
      最近更新 更多