目标C问题:
我有一个 ViewController 的超类和一个数据控制器的超类。数据控制器有委托通知 ViewController 何时更新。这一切似乎都有效,但是在输入一些日志之后,委托方法似乎每次都被调用两次。我想我实际上可能有每个 SubDataController 的两个实例。我一定在某处实例化了一些错误。谁能看到我做错了什么?
我有一个数据密集型应用程序的单独屏幕。我设置了它,以便有一个 UIViewController 的超类,它们都继承自:
DmsProgressBaseViewController.h
@class DmsProgressDataController;
@interface DmsProgressBaseViewController : UIViewController
@property(readonly,assign,nonatomic)DmsProgressDataController *dataController;
//index, so we be identified in container
@property(assign,nonatomic)NSInteger index;
@end
DmsProgressBaseViewController.m
@implementation DmsProgressBaseViewController
@synthesize dataController=_dataController;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(DmsProgressDataController*)dataController
{
if(!_dataController){
_dataController=[[DmsProgressDataController alloc]init];
}
return _dataController;
}
.....
@end
如您所见,每个视图都有一个数据控制器,它通过休息服务加载数据并将其导入核心数据。它也继承自:DmsProgressDataController.h
@class AFHTTPRequestOperation;
@class DmsProgressDataController;
@protocol DmsProgressDataControllerDelegate
-(void)dataLoadedSuccessfully;
@end
@interface DmsProgressDataController : NSObject
@property(nonatomic,assign)id delegate;
@property(nonatomic, strong)NSString *resourceUrl;
-(void)loadDataFromRest;
-(void)loadDataFromRest:(NSString*)resourceUrl;
-(void)handleRestSuccess:(id)jsonResponse;
-(void)handleRestError:(NSError*)error forOperation:(AFHTTPRequestOperation*)operation;
//delegate method
-(void)notifyDataLoadedSuccessfully;
@end
DmsProgressDataController.m @implementation DmsProgressDataController
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(void)loadDataFromRest
{
NSLog(@"loading %@",self.resourceUrl);
if(self.resourceUrl!=nil){
[self loadDataFromRest:self.resourceUrl];
}
}
....
#pragma mark - delegate methods
-(void)notifyDataLoadedSuccessfully
{
if(self.delegate!=nil){
[self.delegate dataLoadedSuccessfully];
}
}
@end
所以那些是超级类......
子类的一个例子是:DmsProgressOverallViewController.h @class DmsProgressOverallDataController;
@interface DmsProgressOverallViewController : DmsProgressBaseViewController
@property(readonly,strong,nonatomic)DmsProgressOverallDataController *dataController;
@end
DmsProgressOverallViewController.m
@interface DmsProgressOverallViewController ()<DmsProgressDataControllerDelegate>
@end
@implementation DmsProgressOverallViewController
@synthesize dataController=_dataController;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.dataController loadDataFromRest:@"overall"];
self.dataController.delegate=self;
}
-(DmsProgressDataController*)dataController
{
if(!_dataController){
_dataController=[[DmsProgressOverallDataController alloc]init];
}
return _dataController;
}
#pragma mark - datacontroller delegate method
-(void)dataLoadedSuccessfully
{
NSLog(@"Overall data loaded successfully");
}
@end
使用数据控制器:
DmsProgressOverallDataController.h
@interface DmsProgressOverallDataController : DmsProgressDataController
@end
DmsProgressOverallDataController.m
@implementation DmsProgressOverallDataController
-(id)init
{
if(self=[super init]){
self.resourceUrl=@"progress/totals";
}
return self;
}
-(void)handleRestSuccess:(id)jsonResponse
{
NSString *defaultClg=[jsonResponse objectForKey:@"defaultClg"];
NSArray *clgs=[jsonResponse objectForKey:@"colleges"];
for(NSString *clg in clgs){
College *college =[College MR_createEntity];
college.collegeName=clg;
if([defaultClg isEqualToString:clg]){
college.defaultClg=[NSNumber numberWithBool:YES];
}
}
//DataController delegate method called here
if(self.delegate!=nil){
[self.delegate dataLoadedSuccessfully];
}
}
@end
我希望这提供了足够的信息。再次感谢您的所有帮助!