Ios 委托不在单例模式下工作

Ios 委托不在单例模式下工作,ios,objective-c,xcode,delegates,singleton,Ios,Objective C,Xcode,Delegates,Singleton,为了在多视图上建立网络连接,我创建了一个单例网络控制器来处理服务器和客户端之间的数据。不幸的是,它不起作用,因为委托方法没有从我的单例视图调用到另一个视图。。下面是我的代码: **单例是SocketIOConnection.h和.m // // SocketIOConnection.h #import <Foundation/Foundation.h> #import "SocketIO.h" #import "SocketIOPacket.h" @protocol Socke

为了在多视图上建立网络连接,我创建了一个单例网络控制器来处理服务器和客户端之间的数据。不幸的是,它不起作用,因为委托方法没有从我的单例视图调用到另一个视图。。下面是我的代码:

**单例是SocketIOConnection.h和.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
//
//SocketIOConnection.h
#进口
#导入“SocketIO.h”
#导入“SocketIOPacket.h”
@协议套接字连接委托
@必需的
-(无效)接收数据包:(id)数据包;
@结束
@接口套接字连接:NSObject{
SocketIO*IO;
id代表;
}
@属性(非原子,保留)IBO插座插座*IO;
@属性(保留)id委托;
+(SocketIOConnection*)共享辛格尔顿;
@结束
//
//SocketIOConnection.m
#导入“SocketIOConnection.h”
@实现SocketIOConnection
@综合IO,代表;
静态SocketIOConnection*shared=NULL;
-(id)init{
if(self=[super init]){
IO=[[SocketIO alloc]initWithDelegate:self];
[IO connectToHost:@“domain.com”端口:443];
}
回归自我;
}
+(SocketIOConnection*)共享辛格尔顿
{
@同步(共享)
{
如果(!shared | | shared==NULL)
{
//分配共享实例,因为它尚未完成
共享=[[SocketIOConnection alloc]init];
}
收益共享;
}
}
-(void)socketIO:(socketIO*)socket direceiveevent:(SocketIOPacket*)数据包{
NSLog(@“委托接收的数据包…”);
[委托接收数据包:packet.dataAsJSON];
}
@结束
这是我的单例代码,下面我将发布我的viewcontroller.h和.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
//
//ViewController.h
#进口
#导入“MBProgressHUD.h”
#导入“SocketIOConnection.h”
@界面ViewController:UIViewController
{
MBHUD*HUD;
}
@属性(非原子,保留)SocketIOConnection*IOConnection;
@结束
//
//ViewController.m
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器{
NSDictionary*运算符数据;
}
@综合会话、连接;
#pragma标记-视图已加载,视图将显示&didReceiveMemoryWarning
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)视图将显示:(BOOL)动画{
IOConnection=[SocketIOConnection sharedSingleton];
//启动验证功能,验证操作员并使其登录。
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”with data:tokenAndLicense];
}

-(void)receivedPacket:(id)packet{看起来您忘记在代码中设置委托。您应该在调用视图控制器的
视图中的singleton后立即执行此操作

-(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];

}

如果您是初学者,请始终检查ARC on并使用属性(非自转、弱)对于自定义委托。是的,ARC处于启用状态。谢谢提示。您在哪里设置委托?@gaige您是什么意思?我正在socketioconnection中创建委托。h?您创建了变量,但它为零,因此向委托发送消息没有任何作用。您需要实际将委托分配到您希望消息传递的位置。很高兴为您服务救命啊-干杯