我正在为iphone编写一个小静态库(lib.a),我正在使用ASIHTTPRequest来管理我的数据发布等。
我有主要的实现(@implementation BlaBla),但在主.m文件中,我有另一个(@interface Foo)和(@implementation Foo)私有方法。
我已经在(@interface Foo)中实现了ASIHTTPRequestDelegate,但是 - (void)requestFinished:(ASIHTTPRequest *)请求,但是这个方法没有执行!
无论我做了什么,它都行不通。我已经添加了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
我会非常感谢任何帮助!
顺便说一句,是不是因为实例方法( - )或类方法(+)?
答案 0 :(得分: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
答案 1 :(得分:0)
不应该是:
BlaRequest.delegate = self;
而不是:
self = [BlaRequest delegate];