20140115更新:使用工作代码
我有一个Singleton,我想使用委托模式。当我与代表调用方法时,我没有得到通知。
我的错误在哪里?如何让委托模式与didComposition
一起使用?
在我的调试器和代码下面,重要部分:
2014-01-15 14:31:09.703 Foobar[5854:70b] -[WebApi sandbox] [Line 42] Sandbox call
2014-01-15 14:31:09.707 Foobar[5854:70b] -[WebApi getSurroundStream] [Line 67] Surround Stream call
#import "AFHTTPRequestOperationManager.h"
@class WebApi;
@protocol WebApiDelegate <NSObject>
-(void)didComposition;
@end
@interface WebApi : AFHTTPRequestOperationManager <SingletonDelegate>
@property (assign, nonatomic)id<WebApiDelegate> delegate;
+(WebApi*)sharedInstance;
-(void)sandbox;
-(void)doSurroundComposition;
@end
#import "WebApi.h"
#define kApiHost @"http://192.168.0.1"
@implementation WebApi
-(WebApi*)initWithBaseURL:url {
self = [super init];
if (self != nil) { }
return self;
}
#pragma mark - Singleton methods
+(WebApi*)sharedInstance
{
static WebApi *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kApiHost]];
});
return sharedInstance;
}
-(void)sandbox {
DLog(@"Sandbox called");
// Do AFNetworking Stuff
}
-(void)doSurroundComposition {
[self sandbox];
DLog(@"do surround composition");
[self.delegate performSelector:@selector(didComposition)];
}
@end
#import <UIKit/UIKit.h>
#import "Lokation.h"
#import "WebApi.h"
@interface SurroundViewController : UICollectionViewController <LokationDelegate, WebApiDelegate>
@property (strong, nonatomic) Lokation *lokation;
@end
#import "SurroundViewController.h"
#import "SWRevealViewController.h"
@interface SurroundViewController ()
@end
@implementation SurroundViewController
-(id)init
{
self = [super init];
if (self) {
// Custom initialization
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[WebApi sharedInstance].delegate = self;
[[WebApi sharedInstance] doSurroundComposition];
}
-(void)didComposition {
DLog(@"did load composition"); // will not be called!
}
@end
答案 0 :(得分:6)
第一次调用[WebApi sharedInstance]
时,您需要将委托设置为某个内容。目前(在您显示的代码中)您没有设置任何委托。因此,当共享实例尝试调用委托时,它只是一个无操作。