3

複数のビューをネットワーク接続するために、サーバーとクライアント間のデータを処理するシングルトン ネットワーク コントローラーを作成しました。残念ながら、デリゲートメソッドが私のシングルトンから他のビューに呼び出されていないため、機能していません..私のコードの下:

** シングルトンは 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

したがって、これは私のシングルトンのコードです。以下に、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

デリゲート メソッドが呼び出されないのはなぜですか?

4

1 に答える 1

8

コードでデリゲートを設定するのを忘れているようです。View Controllerの でシングルトンを呼び出した直後にこれを行う必要があります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];

}
于 2013-04-13T23:40:08.663 に答える