【问题标题】:Delegate not working in singleton [closed]代表不在单例中工作[关闭]
【发布时间】:2013-04-13 23:28:23
【问题描述】:

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

** 单例是 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

为什么我的委托方法没有被调用?

【问题讨论】:

  • 如果你是初学者。始终检查 ARC 并为自定义委托使用属性(非命名,弱)。
  • 是的,ARC 已开启,感谢您的提示。
  • 你在哪里设置代理?
  • @gaige 你是什么意思?我正在 socketioconnection.h 中创建委托?
  • 你创建了变量,但它是 nil,所以向代理发送消息什么都不做。您需要将代理实际分配到您希望消息到达的位置。

标签: ios objective-c xcode delegates singleton


【解决方案1】:

您似乎忘记在代码中设置委托。您应该在视图控制器的 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];

}

【讨论】:

  • 很高兴为您提供帮助。 -干杯
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 2015-07-27
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2016-03-07
  • 1970-01-01
相关资源
最近更新 更多