问题描述
要通过我的多视图进行网络连接,我创建了一个Singleton网络控制器来处理服务器和客户端之间的数据。不幸的是,它没有工作,因为委托方法没有从我的单身被调用到另一个视图..在我的代码下面:
To have a network connection over my multiple view's, I created a Singleton network controller to handle the data between the server and the client. Unfortunately, it is not working because the delegate method is not being called from my singleton to the other view.. Below my code:
**单例是SocketIOConnection。 h和.m
** The singleton is the SocketIOConnection.h and .m
//
// SocketIOConnection.h
#import <Foundation/Foundation.h>
#import "SocketIO.h"
#import "SocketIOPacket.h"
@protocol SocketIOConnectionDelegate <NSObject>
@required
- (void) receivedPacket:(id)packet;
@end
@interface SocketIOConnection : NSObject <SocketIODelegate> {
SocketIO *IO;
id <SocketIOConnectionDelegate> delegate;
}
@property (nonatomic, retain) IBOutlet SocketIO *IO;
@property (retain) id <SocketIOConnectionDelegate> delegate;
+ (SocketIOConnection *)sharedSingleton;
@end
//
// SocketIOConnection.m
#import "SocketIOConnection.h"
@implementation SocketIOConnection
@synthesize IO, delegate;
static SocketIOConnection *shared = NULL;
-(id)init {
if (self = [super init]) {
IO = [[SocketIO alloc] initWithDelegate:self];
[IO connectToHost:@"domain.com" onPort:443];
}
return self;
}
+ (SocketIOConnection *)sharedSingleton
{
@synchronized(shared)
{
if ( !shared || shared == NULL )
{
// allocate the shared instance, because it hasn't been done yet
shared = [[SocketIOConnection alloc] init];
}
return shared;
}
}
-(void)socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
NSLog(@"Delegating received packet..");
[delegate receivedPacket:packet.dataAsJSON];
}
@end
所以这是代码我的单身,下面我会发布我的viewcontroller.h和.m的代码。
So this is the code of my singleton, below i will post the code of my viewcontroller.h and .m
//
// ViewController.h
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#import "SocketIOConnection.h"
@interface ViewController : UIViewController <MBProgressHUDDelegate, SocketIOConnectionDelegate>
{
MBProgressHUD *HUD;
}
@property (nonatomic, retain) SocketIOConnection *IOConnection;
@end
//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
NSDictionary *operatorData;
}
@synthesize SESSION, IOConnection;
#pragma mark - view Did Load, View Will Appear & didReceiveMemoryWarning
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated {
IOConnection = [SocketIOConnection sharedSingleton];
// fire auth function to verify the operator and sign him in.
NSMutableDictionary *tokenAndLicense = [[NSMutableDictionary alloc] init];
[tokenAndLicense setValue:[operatorData objectForKey:@"token"] forKey:@"__ca.token"];
[tokenAndLicense setValue:[operatorData objectForKey:@"license"] forKey:@"__ca.license"];
[IOConnection.IO sendEvent:@"auth" withData:tokenAndLicense];
}
-(void)receivedPacket:(id)packet { <<<< void that should be fired because of the delegate
NSLog(@"Receive ieks..");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
为什么我的委托方法不被调用?
Why is my delegate method not getting called?
推荐答案
看起来你已经忘记在代码中设置委托。您应该在视图控制器的 ViewWillAppear
中调用单例后执行此操作。
It looks like you have forgotten to set the delegate in your code. You should probalby do this right after you call the singleton in the view controller's ViewWillAppear
.
-(void)viewWillAppear:(BOOL)animated {
IOConnection = [SocketIOConnection sharedSingleton];
IOConnection.delegate = self;
// fire auth function to verify the operator and sign him in.
NSMutableDictionary *tokenAndLicense = [[NSMutableDictionary alloc] init];
[tokenAndLicense setValue:[operatorData objectForKey:@"token"] forKey:@"__ca.token"];
[tokenAndLicense setValue:[operatorData objectForKey:@"license"] forKey:@"__ca.license"];
[IOConnection.IO sendEvent:@"auth" withData:tokenAndLicense];
}
这篇关于代表不在单身人士工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!