我正在使用AFNetworking来处理我的应用程序的请求/响应任务。在我的一个处理程序类中,当HTTP请求成功时,我必须执行委托方法调用以更新UI(请注意,我特别不想使用NSNotificationCenter)。但是,编译器抱怨代理没有被声明。我必须以某种方式继续忽略它,并犯同样的错误。这里是我的问题的代码的简化版本:iOS如何使用块内的委托方法
//MYHandler.h
@protocol MYDelegate <NSObject>
-(void) myDelegateMethod;
@end
@interface MYHandler : NSObject
@property (strong, nonatomic) id<MYDelegate> delegate;
@end
//MYHandler.m
@implementation MYHandler
-(void) doSomething
{
//Create and configure request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess: successBlock failure:failureBlock];
[operation start];
}
//Success and Failure
typedef void(^SuccessBlock)(AFHTTPRequestOperation *operation, id responseObject);
typedef void(^FailureBlock)(AFHTTPRequestOperation *operation, NSError *error);
SuccessBlock successBlock = ^(AFHTTPRequestOperation *operation, id responseObject) {
[_delegate myDelegateMethod];
//^this says _delegate is an undeclared identifier
};
FailureBlock failureBlock = ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
};
@end
//MyViewController.h
@interface MyViewController : UIViewController <MYDelegate>
@end
//MyViewController.m
-(void) myDelegateMethod
{
//
}
我已经试过几乎所有和我不能管理运行到你的错误。 在你的代码中,你的协议是在一个单独的文件中声明的? – 2014-09-01 15:16:44