我正在为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
我将非常感谢您的帮助!
顺便说一句,是因为实例方法(-)还是类方法(+)?
最佳答案
您从类方法调用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
关于objective-c - ASIHTTPRequest requestFinished未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10547787/