1

我正在为 iphone 编写一个小静态库 (lib.a),我正在使用 ASIHTTPRequest 来管理我的数据发布等。

我有主实现(@implementation BlaBla),但在主 .m 文件中,我有另一个(@interface Foo)和(@implementation Foo)用于私有方法。

我已经在 (@interface Foo) 中实现了 ASIHTTPRequestDelegate,但是 - (void)requestFinished:(ASIHTTPRequest *)request,但是这个方法没有执行!

不管我做什么,它都不起作用。我添加了 NSLog 来记录 requestFinished 方法,但它不起作用。

示例代码:

@interface  ActionsManager : NSObject <ASIHTTPRequestDelegate>


+ (void)blabla; 


@end



@implementation ActionsManager 


+ (void)blabla{

    NSURL *requestUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Bla.com/bla"]];

    BlaRequest = [[[ASIFormDataRequest alloc] initWithURL:requestUrl]autorelease];

    self = [BlaRequest delegate];
    [BlaRequest setPostValue:blabla forKey:@"bla"];
     [BlaRequest setRequestMethod:@"POST"];
    [BlaRequest startAsynchronous];


}



- (void)requestFinished:(ASIHTTPRequest *)request{
      NSLog(@"request finished");

}

- (void)requestStarted:(ASIHTTPRequest *)request{

    NSLog(@"request started");

}


@end


@implementation MainImplementation 



- (id)init
{    
    self = [super init];
    if (self) {

    }

    return self;

}

+ (void)bbb{

[ActionsManager blabla];

}

@end

我会非常感谢任何帮助!

顺便说一句,可能是因为实例方法(-)还是类方法(+)?

4

2 回答 2

1

你从一个类方法调用 self ,你的代码应该是这样的:

@interface  ActionsManager : NSObject <ASIHTTPRequestDelegate>
- (void)blabla; 
@end

@implementation ActionsManager 


    - (void)blabla{

        NSURL *requestUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Bla.com/bla"]];

        ASIFormDataRequest *blaRequest = [ASIFormDataRequest requestWithURL:requestUrl];

        blaRequest.delegate = self;
        [blaRequest setPostValue:@"blabla" forKey:@"bla"];
        [blaRequest setRequestMethod:@"POST"];
        [blaRequest startAsynchronous];


    }



    - (void)requestFinished:(ASIHTTPRequest *)request{
          NSLog(@"request finished");

    }

    - (void)requestStarted:(ASIHTTPRequest *)request{

        NSLog(@"request started");

    }


    @end


    @implementation MainImplementation 



    - (id)init
    {    
        self = [super init];
        if (self) {

        }

        return self;

    }

    + (ActionsManager *)bbb{

    ActionsManager *a = [ActionsManager new];
    [a blabla];
    return [a autorelease]; 
    }

    @end
于 2012-05-11T08:39:23.390 回答
0

不应该是:

  BlaRequest.delegate = self;

代替:

  self = [BlaRequest delegate];
于 2012-05-11T08:19:55.070 回答